Sword refers to offer-41-Sum of two numbers VS and a continuous positive sequence of s-java

Questions and tests

package sword041;
/* 
题目一:输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。假设有多个数字的和等于s,输出随意一对就可以。

比如输入数组{1,2,4,7,11,15}和数字15.因为4+11=15。因此输出4和11.

题目二:输入一个正数s,打印出全部的和为s的连续正数序列(至少含有两个数字)。

比如输入15。因为1+2+3+4+5=4+5+6=7+8=15,所以结果打印出3个连续的序列1——5,4——6。7——8.

*/

import java.util.List;

public class main {
	
	public static void main(String[] args) {
		int[][] testTable = {
   
   {1,2,4,7,11,15},{-1,-100,3,99}};
		int[] testTable2={15,1};
		for (int i=0;i<testTable.length;i++) {
			test(testTable[i],testTable2[i]);
		}
		
		int[] testTable3 = {15,10,6};
		for (int ito : testTable3) {
			test2(ito);
		}
	}
		 
	private static void test(int[] ito,int ito2) {
		Solution solution=new Solution();
		long begin = System.currentTimeMillis();
		for (int i = 0; i < ito.length; i++) {
		    System.out.print(ito[i]+" ");		    
		}
		System.out.println();
		//开始时打印数组
		System.out.println(ito2);
		
		System.out.println( "rtn=" );
		solution.findNumbersWithSum(ito,ito2);//执行程序
		long end = System.currentTimeMillis();	

		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}
	
	private static void test2(int ito) {
		Solution solution = new Solution();
		long begin = System.currentTimeMillis();
		System.out.print(ito);		    
		System.out.println();
		//开始时打印数组
		
		System.out.println( "rtn=" );
		solution.findContinuousSequence(ito);//执行程序
		long end = System.currentTimeMillis();			

		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

Topic 1 (successful)

Enter an array in ascending order and a number s, and find two numbers in the array so that their sum is exactly s. If the sum of multiple numbers is equal to s, just output any pair.

For example, the input array {1, 2, 4, 7, 11, 15} and the number 15. Since 4+11=15, 4 and 11 are output.

Many people will immediately think of the O(n2) method, that is, fix a number in the array first, and then determine whether the sum of the remaining n-1 numbers in the array and its sum is equal to s.

Then we look for better algorithms. We first select two numbers in the array, if their sum is equal to the input s, we have found the two numbers we are looking for. What if it is less than s? We hope that the sum of the two numbers will be bigger. Since the array is already sorted, we can consider choosing the number after the smaller number. Because the number at the back is bigger, then the sum of these two numbers should be bigger too, it may be equal to the input number s. Similarly, when the sum of the two numbers is greater than the number entered, we can choose the number before the larger number, because the number in front is smaller.

Topic 2 (successful)

Enter a positive number s, and print out all consecutive positive number sequences (contain at least two numbers) that sum to s. For example, input 15, because 1+2+3+4+5=4+5+6=7+8=15, so the result prints out 3 consecutive sequences 1——5,4——6,7——8 .

With the previous experience, we also consider using two trees, small and big, to represent the minimum and maximum values ​​of the sequence, respectively. First initialize small to 1, and big to 2. If the sum of the sequence from small to big is greater than s, we can remove the smaller value from the sequence, that is, increase the value of small. If the sum of the sequence from small to big is less than s, we can increase big to make this sequence contain more numbers. Because this sequence must have at least two numbers, we keep increasing small until (1+s)/2.

package sword041;


class Solution {
    public void findNumbersWithSum(int[] nums, int k) {
        int length=nums.length;
        if(length==0 || length == 1){
        	return;
        }
        int begin = 0;
        int end = length -1;
        while(begin <= end) {
        	int sum = nums[begin] + nums[end];
        	if(sum == k) {
        		System.out.println(nums[begin] + " "+ nums[end]);
        		return;
        	}
        	if(sum > k) {
        		end--;
        	}else {
        		begin++;
        	}
        }        
    }
    
    public void findContinuousSequence(int k) {
    	if(k <= 2) {
    		return;
    	}
        int begin = 1;
        int end = 2;
        int sum = 3;
        while(begin <= end && end <= k) {
        	if(sum == k) {
        		if(begin != end) {
        			System.out.println(begin + " "+ end);	
        		}
        		end++;
        		sum+=end;
        		continue;
        	}
        	if(sum > k && begin != end) {        		
        		sum-=begin;
        		begin++;
        	}else {
        		end++;
        		sum+=end;
        	}
        }
           
    }
}

 

Guess you like

Origin blog.csdn.net/xushiyu1996818/article/details/112170210