当前位置:网站首页>Leetcode 1995. Statistics special quads (brute force enumeration)

Leetcode 1995. Statistics special quads (brute force enumeration)

2022-06-11 10:31:00 I'm not xiaohaiwa~~~~

 Insert picture description here
To give you one Subscript from 0 Start Array of integers for nums , Returns a that meets the following conditions Different Four tuple (a, b, c, d) Of number :

  • nums[a] + nums[b] + nums[c] == nums[d] , And
  • a < b < c < d

Example 1:

 Input :nums = [1,2,3,6]
 Output :1
 explain : The only Quad that meets the requirements is  (0, 1, 2, 3)  because  1 + 2 + 3 == 6 .

Example 2:

 Input :nums = [3,3,6,4,5]
 Output :0
 explain :[3,3,6,4,5]  There are no required quads in the .

Example 3:

 Input :nums = [1,1,1,3,5]
 Output :4
 explain : Meet the requirements of  4  The four tuples are as follows :
- (0, 1, 2, 3): 1 + 1 + 1 == 3
- (0, 1, 3, 4): 1 + 1 + 3 == 5
- (0, 2, 3, 4): 1 + 1 + 3 == 5
- (1, 2, 3, 4): 1 + 1 + 3 == 5

Tips :

  • 4 <= nums.length <= 50
  • 1 <= nums[i] <= 100

Code:

class Solution {
    
public:
    int countQuadruplets(vector<int>& nums) {
    
        int cnt=0;
        for(int i=0;i<nums.size();i++)
        {
    
            for(int j=i+1;j<nums.size();j++)
            {
    
                for(int k=j+1;k<nums.size();k++)
                {
    
                    for(int l=k+1;l<nums.size();l++)
                    {
    
                        if((nums[i] + nums[j] + nums[k]) == nums[l] )
                        {
    
                            cnt++;
                        }
                    }
                }
            }
        }
        return cnt;
    }
};
原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111028393963.html