The Dream Of leozp

try my best

Leetcode: 4Sum

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int> >answer;
        set<vector<int> >ans;
        int n=nums.size();
        if(n<4){
            return answer;
        }
        sort(nums.begin(),nums.end());
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                int sum=nums[i]+nums[j];
                int flag=target-sum;
                int begin=j+1,end=n-1;
                
                while(begin<end){
                if(nums[begin]+nums[end]==flag){
                    vector<int>Q;
                    Q.push_back(nums[i]);
                    Q.push_back(nums[j]);
                    Q.push_back(nums[begin]);
                    Q.push_back(nums[end]);
                    ans.insert(Q);
                    begin++;
                    end--;
                }
                else if(nums[begin]+nums[end]>flag){
                    end--;
                }
                else{
                    begin++;
                 }
                }
            }
        }
                set<vector<int> >::iterator iter=ans.begin();
                for(;iter!=ans.end();iter++){
                    answer.push_back(*iter);
                }
            
        
        return answer;
    }
};

阅读更多
版权声明:本文为博主原创文章,欢迎指教~ https://blog.csdn.net/u013712847/article/details/52386540
文章标签: leetcode
个人分类: leetcode
上一篇Leetcode: Letter Combinations of a Phone Number
下一篇Leetcode: Remove Nth Node From End of List
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭