Comic: How to find two numbers whose sum is a "specific value" in an array?

----- the next day -----

What does that mean? Let's take an example, given the following integer array (the title assumes that there are no duplicate elements in the array):

We randomly choose a specific value, such as 13, and ask to find all combinations where the sum of two numbers is equal to 13 .

Since 12+1 = 13, 6+7 = 13, the final output result (the output is a subscript) is as follows:

【1, 6】

【2, 7】

The idea that Xiao Hui wants to express is to traverse the entire array directly, and add to other elements every time an element is traversed to see if the sum is equal to that specific value.

In the first round , add element 5 and other elements:

Two elements that meet the requirements were not found.

In the second round , add element 12 and other elements:

It is found that the result of adding 12 and 1 is 13, which meets the requirements.

According to this idea, the entire array has been traversed.


————————————



Let us demonstrate in detail:

In the first round , visit element 5 and calculate 13-5=8. Look up 8 in the hash table and find that it can't be found:

In the second round , visit element 12 and calculate 13-12=1. Look up 1 in the hash table and find that the subscript of element 1 is 6, so element 12 (subscript is 1) and element 1 (subscript is 6) are a pair of results:

In the third round , visit element 6, and calculate 13-6=7. Look up 7 in the hash table and find that the subscript of element 7 is 7, so element 6 (subscript is 2) and element 7 (subscript is 7) are a pair of results:

According to this idea, just traverse the entire array all the time.

public class FindSumNumbers {

    public static List<List<Integer>> twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        List<List<Integer>> resultList = new ArrayList<>();
        for (int i = 1; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int other = target - nums[i];
            if (map.containsKey(other) && map.get(other) != i) {
                resultList.add(Arrays.asList(i,map.get(other)));
                //为防止找到重复的元素对,匹配后从哈希表删除对应元素
                map.remove(nums[i]);
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        int[] nums = {5,12,6,3,9,2,1,7};
        List<List<Integer>> resultList = twoSum(nums, 13);
        for(List<Integer> list : resultList){
            System.out.println(Arrays.toString(list.toArray()));
        }
    }

}

    public static List<List<Integer>> twoSumV2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        List<List<Integer>> resultList = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int other = target - nums[i];
            if (map.containsKey(other)) {
                resultList.add(Arrays.asList(map.get(other),i));
            }
            map.put(nums[i], i);
        }
        return resultList;
    }

The Mid-Autumn Festival is approaching, Xiao Hui prepared a welfare for everyone ,

Scan the code to follow the official account below, reply to the keyword "milk tea" , you can participate in the lottery:

点个[在看],是对小灰最大的支持!

Guess you like

Origin blog.csdn.net/bjweimengshu/article/details/108860301