Java is given an integer array nums and an integer target value target, please find the two integers in the array whose sum is the target value target, and return their array subscripts.

//Given an integer array nums and an integer target value target, 
// Please find the two integers in the array whose sum is the target value target, and return their array subscripts. 
//You can assume that there will only be one answer for each input. However, the same element in the array cannot be repeated in the answer. 
// You can return the answers in any order. 
//Example 1: 
//input: nums = [2,7,11,15], target = 9 
//output: [0,1] 
//explanation: because nums[0] + nums[1] == 9 , which returns [0, 1] .

public class Test38 {
    public static void main(String[] args) {
        int[] arr={2,7,11,15};
        int target=9;
        findIt(arr,target);
    }

    private static void findIt(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i; j < arr.length; j++) {
                if((arr[i]+arr[j])==target){
                    System.out.println("["+i+","+j+"]");
                }
            }

        }
    }
}

Achieving the result:

 

Guess you like

Origin blog.csdn.net/m0_62218217/article/details/121444193