[Prove safety Offer] 57- s and the number of

A topic

Enter an increasing array and a number, find the two numbers in the array, and they are just as S, and if a plurality of pairs of S, then output any pair can be.

answer

The key is an array of information ordered. Initializing i, j points to the first and second number, compared with the S, if smaller, - j, if big, ++ i. Discard elements and other means that it can no longer count the number of the sum equal to the target, taking advantage of ordering. Time complexity of O (n).

Code

import java.util.ArrayList;

public class Main {
    public static void main(String args[]) {
        int[] arr= {1,2,4,7,11,15};
        int sum=15;
        ArrayList<Integer> ansList=findNumbersWithSum(arr,sum);
        if(ansList!=null) {
            System.out.println(ansList);
        }
        else {
            System.out.println("none");
        }

    }
    
    public static ArrayList<Integer> findNumbersWithSum(int [] array,int sum) {
        if(array.length<2) {
            return null;
        }
        int i=0;
        int j=array.length-1;
        while(i<j) {
            if(array[i]+array[j]==sum) {
                break;
            }
            else if(array[i]+array[j]<sum) {
                ++i;
            }
            else {
                --j;
            }       
        }
        if(i!=j) {
            ArrayList<Integer> list=new ArrayList<>();
            list.add(array[i]);
            list.add(array[j]);
            return list;
        }
        else {
            return null;
        }
    }
}

Topic two

Enter a positive number s, and print out all consecutive positive sequence s (including the number of at least 2.

answer

  • Using the idea of ​​a subject, but this problem is noted that the number of consecutive> = 2 to the number. Initialization num1 = 1, num2 = 2, num1 mean that the small number. And compare goals and, if big ++ num2, equal and on the + num2; if smaller ++ num1, equal and on - the num1.
  • Note that the termination condition num1 <(1 + s) / 2.
  • The time complexity of their count should be O (n ^ 2).

Code

import java.util.ArrayList;

public class Main {
    public static void main(String args[]) {
        int sum=15;
        ArrayList<ArrayList<Integer>> list=findContinuousSequence(sum);
        if(list!=null) {
            for(ArrayList<Integer> seq:list) {
                for(int num:seq) {
                    System.out.print(num);
                }
                System.out.println("\n");
            }
        }
        else {
            System.out.print("null");
        }
    }
    
    public static ArrayList<ArrayList<Integer>> findContinuousSequence(int sum) {
        if(sum<3) {
            return null;
        }
        
        int i=1;
        int j=2;
        int s=i+j;
        ArrayList<ArrayList<Integer>> sequenceList=new ArrayList<>(); 
        while(i<(1+sum)/2) {//
            if(s==sum) {
                sequenceList.add(getAnsList(i,j));
                ++j;
                s+=j;
            }
            else if(s<sum) {
                ++j;
                s+=j;
            }
            else {
                s-=i;//注意顺序
                ++i;//
            }
        }
        return sequenceList;
    }
    
    public static ArrayList<Integer> getAnsList(int i,int j){
        ArrayList<Integer> list=new ArrayList<>();
        for(int num=i;num<=j;++num) {
            list.add(num);
        }
        return list;
    }
}

Guess you like

Origin www.cnblogs.com/coding-gaga/p/11089224.html