zoukankan      html  css  js  c++  java
  • 15、3Sum

    Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

    Note: The solution set must not contain duplicate triplets.

    For example, given array S = [-1, 0, 1, 2, -1, -4],
    
    A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]
    

    第一遍写了下面的代码,代码问题是运行过程中

    输入[-1,0,1,2,-1,-4]

    输出结果为:[[-1,0,1],[-1,2,-1],[0,1,-1]](似乎没有什么问题)

    但是标准答案要的结果是:[[-1,-1,2],[-1,0,1]] 。

    所以[-1,0,1]与[0,1,-1]看做是重复的,应该只留一个。

     有问题代码一:

    class Solution {
    public:
        vector<vector<int>> threeSum(vector<int>& nums) {
            vector<vector<int>> answers;
            int n=0;
            if(nums.empty() || nums.capacity() < 3) return answers;
            for(int i=0; i < nums.capacity() - 2; i++)
                for(int j = i + 1; j < nums.capacity() - 1; j++)
                    for(int k = j + 1; k < nums.capacity(); k++)
                        if(nums[i] + nums[j] + nums[k] == 0){
                            vector<int> answer;
                            answer.push_back(nums[i]);
                            answer.puh_back(nums[j]);
                            answer.push_back(nums[k]);
                            answers.push_back(answer);
                        }   
            return answers;
        }
    };

     

     

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

    第二遍先对nums排序,加了两行,但是

    输入:[0,0,0,0]
    运行输出是:[[0,0,0],[0,0,0],[0,0,0]]
    显然重复,应该输出:[[0,0,0]]才对
    所以这次代码提交也没通过:
     
    有问题代码二:
    class Solution {
    public:
        vector<vector<int>> threeSum(vector<int>& nums) {
            vector<vector<int>> answers;
            std::sort(nums.begin(), nums.end());  //对nums排序
            int n=0,flag=0;
            if(nums.empty() || nums.capacity() < 3) return answers;
            for(int i=0; i < nums.capacity() - 2; i++){
                if(i>0 && nums[i] == nums[i - 1]) continue;   //如果当前元素等于前一个元素,跳过,防止出现重复配对
                for(int j = i + 1; j < nums.capacity() - 1; j++)
                    for(int k = j + 1; k < nums.capacity(); k++)
                        if(nums[i] + nums[j] + nums[k] == 0){
                            vector<int> answer;
                            answer.push_back(nums[i]);
                            answer.push_back(nums[j]);
                            answer.push_back(nums[k]);
                            answers.push_back(answer);
                        }   
            }
            return answers;
        }
    };
     
     ————————————————————————————————————————————————————————————————————————
     
    最终没做对,做了一两个小时没做对。。。。看官网讨论区的答案吧:
    class Solution {
    public:
       vector<vector<int> > threeSum(vector<int> &num) {
        
        vector<vector<int> > res;
    
        std::sort(num.begin(), num.end());
    
        for (int i = 0; i < num.size(); i++) {
            
            int target = -num[i];
            int front = i + 1;
            int back = num.size() - 1;
    
            while (front < back) {
    
                int sum = num[front] + num[back];
                
                // Finding answer which start from number num[i]
                if (sum < target)
                    front++;
    
                else if (sum > target)
                    back--;
    
                else {
                    vector<int> triplet(3, 0);
                    triplet[0] = num[i];
                    triplet[1] = num[front];
                    triplet[2] = num[back];
                    res.push_back(triplet);
                    
                    // Processing duplicates of Number 2
                    // Rolling the front pointer to the next different number forwards
                    while (front < back && num[front] == triplet[1]) front++;
    
                    // Processing duplicates of Number 3
                    // Rolling the back pointer to the next different number backwards
                    while (front < back && num[back] == triplet[2]) back--;
                }
                
            }
    
            // Processing duplicates of Number 1
            while (i + 1 < num.size() && num[i + 1] == num[i]) 
                i++;
    
        }
        
        return res;
        
    }
    };
     
  • 相关阅读:
    xshell下载官网地址
    logo
    网站案例搜集
    cnblogs修改网站图标icon
    父页面调用子页面方法, 子页面加载父页面传送的数据
    对Java的常用对象(POJO、DTO、PO、BO、VO、DAO)详细解释及应用场景
    关于jsp发起请求加载datagrid数据(草稿)
    接口测试-Http状态码-postman上传文件
    httpclient获取响应实体和信息的封装方法(解耦更新)
    使用httpClient调用接口获取响应数据
  • 原文地址:https://www.cnblogs.com/hozhangel/p/7811217.html
Copyright © 2011-2022 走看看