The addition of four numbers in go language is equal to the specified number algorithm

This article mainly introduces the operation of adding four numbers equal to the specified number algorithm in go language, which has a good reference value and hopes to be helpful to everyone. Let's come and see with me

Given four array lists A , B , C , D containing integers, calculate how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D [l] = 0.

First divide the four arrays into pairs of arrays, add the values ​​of the first two arrays, add the last two arrays, add the sum of the first two arrays and the sum of the last two arrays for shares, and the sum is exactly the opposite number, four elements The sum is 0.

first:

The elements of the two arrays are traversed and added, and the sum is the index of the map. The element pointed to is the number of occurrences.

1

2

3

4

5

6

7

8

func foursumcount(A []int, B []int, C []int, D []int) int{

 des :=map[int]int{}

 for _,v:=range A{

  for _,w:=range B{

   des[v+w]++

  }

 }

}

Traverse the other two arrays again, add the elements of the two arrays, take the opposite number of the sum, and search in the map by using the opposite number, if it does not appear, the pointed number is 0, if the opposite of this number appears number, the number pointed to is greater than one.

1

2

3

4

5

6

7

8

9

func foursumcount(A []int, B []int, C []int, D []int) int{

 des :=map[int]int{}

 ans:=0

 for _,v:=range C{

  for _,w:=range D{

   ans +=des[-v-w]

  }

 }

}

Finally return the total

all codes

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

func fourSumCount(A []int, B []int, C []int, D []int) int {

 des := map[int]int{}

 ans:=0

 for _,v :=range A{//遍历两个数组,将两个数组的和作为一个索引,进行+1操作

  for _,w:=range B{

    des[v+w]++

  }

 }

 for _,v :=range C{//遍历另两个数组,如果这两个数组进行相加的和的相反数在map中不为1,则证明出现过

  for _,w:=range D{

   ans +=des[-v-w]

  }

 }

 return ans//返回总数

}

Supplement: Algorithm question: The addition of three numbers is equal to a certain value

The title comes from the fifteenth question of leetcode

Given an array S of n integers, is there an element a, b, c in S such that a + b + c = 0? Find all unique triples in the array that sum to zero.

Note: Solution sets cannot contain duplicate triples.

example:

Given an array:

1

S = [-1, 0, 1, 2, -1, -4]

solution:

1

[[-1, 0, 1],[-1, -1, 2]]

When I first saw the title of this question, the first thing I thought of was the violent solution. After sorting the array, I directly nested three loops. Although this is simple, the time complexity is indeed n^3, and the amount of data is too large. The time consumption was too large, and it was not passed when submitting.

After thinking about it for a while, I came up with some optimization schemes, but none of them essentially reduced the power, so improvement is still needed, and the goal is n^2.

First of all, if the target is n^2, the array needs to be scanned twice. There is no problem with the first layer of loops, but the second and third layers of loops need to be reduced to one scan, because the addition of two numbers is equal to A certain value, so the ordered array can be scanned from front to back and from back to front until they meet. If the loop continues after the meeting, the obtained results will be repeated.

So you can jump out of the loop after you meet. In this way, it only needs to scan the array once to achieve the result of two layers of loops. The idea is simple. Some other issues should be considered when implementing it. The specific implementation code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

public class Solution {

    public List<List<Integer>> threeSum(int[] nums) {

        List<List<Integer>> result = new LinkedList<List<Integer>>();

        if(nums.length<3){

            return result;

        }

        Arrays.sort(nums);

        int left=0,right=nums.length-1;

        for(int mid=0;mid< nums.length-2;mid++){

            if(nums[mid]>0) break;

            if(mid == 0 || (mid > 0 && nums[mid] != nums[mid-1])){

                left=mid+1;

                right=nums.length-1;

                while(left<right){

                    if(nums[left]+nums[mid]+nums[right] ==0){

                        result.add(Arrays.asList(nums[mid],nums[left],nums[right]));

                        while (left < right && nums[left] == nums[left+1]) left++;

                        while (left < right && nums[right] == nums[right-1]) right--;

                        left++;

                        right--;

                    }else if(nums[left]+nums[mid]+nums[right]<0){

                        left++;

                    }else if(nums[left]+nums[mid]+nums[right]>0){

                        right--;

                    }

                }

            }

        }

        return result;

    }

}

以上为个人经验,希望能给大家一个参考,也希望可以帮到你。

转自:微点阅读   https://www.weidianyuedu.com  

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131830289