Given an array of integers and a target value, and to find the two numbers in the array, for example, a target value given nums = [2,7,11,15], target = 9

python Solutions

= the nums [1,2,3,4,5,6] # If this array is the given 
target. 9 =            # If this is the given target value 
num_list = []         # used to hold results container 
DEF RUN (the nums , target):
     '' ' performance function ' '' 
    for num1 in the nums:
         for num2 in the nums:
             IF num1 num2 + == target: 
                num_list.append (num1) 
                num_list.append (num2) 
                Print (num_list)
                 BREAK 
        IF num_list:
             BREAK 




IF  the __name__ == '__main__':
    run(nums,target)

java solutions

Import of java.util.ArrayList;
 public  class Demo01 {
     public  static  void main (String [] args) {
         // for storing the detected data 
        the ArrayList <Integer> = the nums new new the ArrayList <> ();
         // assuming that the data 
        nums.add (. 1 ); 
        nums.add ( 2 ); 
        nums.add ( . 3 ); 
        nums.add ( . 6 ); 
        nums.add ( . 7 ); 
        nums.add ( . 5 );
         // assuming that the target data It is. 9 
        int target. 9 =;
         // call the function 
        the getResult (the nums, target); 
    } 
public  static  void the getResult (the ArrayList <Integer> the nums, int target) {
         // for storing data and the results of 
        the ArrayList <Integer> = Result new new the ArrayList <> ();
         / / set flag 
        Boolean Status = to false ;
         for ( int I = 0; I <nums.size (); I ++ ) {
             for ( int I1 = 0; I1 <nums.size (); I1 ++ ) {
                 IF (nums.get ( i) + nums.get (i1) == target) {
                    result.add(nums.get(i));
                    result.add(nums.get(i1));
                    status = true;
                    System.out.println(result);
                    break;
                }
            }
            if (status) {
                break;
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/117698ai/p/11141180.html