leetcode-3Sum-15

输入一个数组,任选三个数求和,和为0的所有组合,不能重复,即,-1,0,1这样的三元组只能出现一次。

解法:类似两个数求和的方法,先排序,然后首尾指针法,但这里要求三个数,所以还是得遍历一遍,因此第一个数遍历,剩下两个数用首尾指针法,ON^2

对于重复三元组的处理:本来想把所有结果先放入set<node> 里,然后在复制到vector<vector<int> >中返回,但不知道为何不能去重。先马再说

 1 class Solution {
 2 // private:
 3 //     struct node{
 4 //         int a,b,c;
 5 //         node(){}
 6 //         node(int x,int y,int z):a(x),b(y),c(z){}
 7 //         bool operator<(const node &n)const{
 8 //             if(a==n.a&&b==n.b&&c==n.c) return false;
 9 //             return true;
10 //         }
11 //     };
12 public:
13     vector<vector<int> > threeSum(vector<int>& nums) {
14         vector<vector<int> > v;
15         if(nums.size()<3) return v;
16         sort(nums.begin(),nums.end());
17         //set<node> s;
18         for(int i=0;i<nums.size();i++){
19             if(i>0&&nums[i]==nums[i-1]) continue;
20             int j=i+1,k=nums.size()-1;
21             while(j<k){
22                 int sum=nums[i]+nums[j]+nums[k];
23                 if(sum==0){
24                     vector<int> vv;
25                     vv.push_back(nums[i]);
26                     vv.push_back(nums[j]);
27                     vv.push_back(nums[k]);
28                     v.push_back(vv);
29     //                 node no(nums[i],nums[j],nums[k]);
30     //                 if(s.find(no)==s.end()){
31                 //     //cout<<"ha"<<endl;
32     //                     s.insert(no);
33     //                 }
34                     int tmp=nums[j];
35                     while(nums[j]==tmp) j++;
36                     continue;
37                 }
38                 if(sum<0) j++;
39                 else k--;
40             }
41         }
42 //         for(set<node>::iterator it=s.begin();it!=s.end();it++){
43 //             vector<int> vv;
44 //             vv.push_back(it->a);
45 //             vv.push_back(it->b);
46 //             vv.push_back(it->c);
47 //             v.push_back(vv);
48 //         }
49        // cout<<"*********"<<endl;
50 //         for(int i=0;i<v.size();i++){
51 //             cout<<v[i][0]<<" "<<v[i][1]<<" "<<v[i][2]<<endl;
52 //         }
53         return v;
54     }
55 };
时间: 2024-11-07 02:27:43

leetcode-3Sum-15的相关文章

leetcode -day 15 Distinct Subsequences

1.  Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters with

[leetcode]3Sum Closest @ Python

原题地址:http://oj.leetcode.com/problems/3sum-closest/ 题意:数组中每三个元素进行求和,找出所有和中大小最接近target的和,并返回这个和与target之间的差值. 解题思路:使用一个变量mindiff来监测和与target之间的差值,如果差值为0,直接返回sum值. 代码: class Solution: # @return an integer def threeSumClosest(self, num, target): num.sort()

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i

leetcode:3sum closet

题目:给一个数组和给定的目标值,要求在数组里找出三个元素,这三个元素的和最接近目标值,当然等于是最好的. 用3sum的方法,把判定条件作些修改. int twoSum(vector<int> &num, int start, int target) { if(num.size() < 3 || start >= num.size()) return -target; int head = start; int tail = num.size() - 1; int resul

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode][Python]15:3Sum

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 15: 3Sumhttps://oj.leetcode.com/problems/3sum/ Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?Find all unique triplets in the array which gives the sum

Leetcode之15. 3Sum (medium)

15. 3Sum (medium) 描述 Given an array nums of n integers, are there elements a, b, c in nums 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. Example

【leetcode】15. 3Sum

题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 解题分析: 这道题注意一下几点即可: 1,先固定前两个数的位置,移动第三个数,这样的查找方式不会有数字组合的遗漏: 2,要考虑到数组元素有重复的情况下的处理. 3,若先为数组排

leetcode第15题--3Sum

Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

LeetCode(15) 3Sum

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so