The sword refers to the offer surface question (Java Edition): And for S two numbers vs and a continuous positive sequence for s

Source: Internet
Author: User

Question one: Enter an array with an ascending order and a number s, and find two numbers in the array so that they are exactly s. If there are multiple numbers and equals s, the output can be any pair.

For example, the input array {1,2,4,7,11,15} and the number 15. Because of 4+11=15, outputs 4 and 11.

In the interview, it is very important that the candidate should show a quick reaction ability. As long as you think of a solution, the candidate can tell the interviewer right away, even though it's not necessarily the best option. For example, many people can immediately think of O (N2) method, that is, to first set a number in the array, and then determine the array of the remaining n-1 numbers with its sum is not equal to S. The interviewer will tell us that this is not the best way. But it doesn't matter, at least the interviewer will know that our thinking is still relatively agile.

Then we look for better algorithms. We first select two numbers in the array, and if they are equal to the input s, we find the two numbers we are looking for. What if it's less than s? We want two numbers and a little bigger. Since the arrays are already sorted, we can consider selecting the numbers that follow the smaller numbers. Because the number in the back is a little larger, the sum of the two numbers is larger, it is possible to equal the number s of the input. Similarly, when two numbers and greater than the input number, we can select the larger number in front of the numbers, because the number in front of the smaller.

Based on the above ideas, we implement the code as follows:

public void Findnumberswithsum (int[] sortedarray, int number) {if (Sortedarray = = null) return; int pointhead = 0;int Pointen D = sortedarray.length-1;while (Pointend > Pointhead) {Long cursum = sortedarray[pointend]+sortedarray[pointhead];if (Cursum = = number) {System.out.println (sortedarray[pointhead]); System.out.println (Sortedarray[pointend]); break;} Else{if (cursum >number) Pointend--;elsepointhead + +;}}}
The time complexity of this algorithm is O (n)


Title Two: Enter a positive number s and print out all successive positive sequences (at least two digits) for S. For example, input 15, because of 1+2+3+4+5=4+5+6=7+8=15, so the result prints out 3 sequential sequence 1--5,4--6,7--8.

With previous experience, we also consider using two tree small and big to represent the minimum and maximum values of the sequence respectively. First initialize the small to 1,big to 2. If the sequence from small to big and greater than s, we can remove the smaller value from the sequence, that is, increase the value of the small. If the sequence from small to big and less than S, we can enlarge big and let the series contain more numbers. Since this sequence has at least two digits, we have increased small to (1+s)/2.

Java code Implementation of the above ideas:

public void findcontinuoussequence (int s) {if (S < 4) return; int small = 1;int Big = 2;while (Small < (s+1)/2) {int Cursu m = 0;for (int i = small; i<=big;i++) cursum +=i;if (cursum = = s) {System.out.println ("find one"); for (int i = Small;i<=b ig;i++) System.out.print (i+ "+"); small++;} Else{if (Cursum > s) small++;elsebig++;}}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sword refers to the offer surface question (Java Edition): And for S two numbers vs and a continuous positive sequence for s

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.