题目

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 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)

分析

求出给定整数序列中三个之和为0的不重复组合序列!

我采用了暴力解决办法,三层循环,但是Status: Time Limit Exceeded~~~

不断思考,得到复杂度为O(n2)的AC代码。

Time Limit Exceeded代码

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vv; int size = nums.size(); if (size < 3)
return vv; for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
for (int k = j + 1; k < size; k++)
{
if (nums[i] + nums[j] + nums[k] == 0)
{
int arr[3] = { nums[i], nums[j], nums[k] };
//将arr中的元素从小到大排序
sort(arr); vector<int> v = { arr, arr + 3 };
if (!find(vv , v))
vv.push_back(v);
}
else{
continue;
}
}//for
}//for
}//for return vv;
} bool find(vector<vector<int>> &vv, vector<int> &v)
{
vector<vector<int>>::iterator iter; for (iter = vv.begin(); iter != vv.end(); iter++)
{
if ((*iter)[0] == v[0] && (*iter)[1] == v[1] && (*iter)[2] == v[2])
{
return true;
}
}
return false;
} void sort(int *a)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3 - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
};

AC代码

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> vv; int size = nums.size(); if (size < 3)
return vv; sort(nums.begin() , nums.end()); for (int i = 0; i < size; i++)
{
//跳过重复数字减少循环
if ((i>0) && nums[i] == nums[i - 1])
continue; int j = i + 1;
int k = size - 1; //如果最大的数还小于0 则直接返回空集合
if (nums[k] < 0)
return vv;
while (j < k)
{
int sum = nums[i] + nums[j] + nums[k]; if (sum == 0)
{
//元素nums[i], nums[j], nums[k]本来就是从小到大排序
int arr[3] = { nums[i], nums[j], nums[k] }; vector<int> v = { arr, arr + 3 }; vv.push_back(v); //跳过重复数字减少循环
while ( (j<k) && (nums[j] == nums[j + 1]))
j++; while ((j<k) && (nums[k] == nums[k - 1]))
k--; j++;
k--;
}
else if(sum < 0){
j++;
}
else{
k--;
}
}
}//for
return vv;
}
};

GitHub测试程序源码

LeetCode(15) 3Sum的更多相关文章

  1. LeetCode(15)3Sum

    题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...

  2. Leetcode(15)-三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  3. LeetCode(15):三数之和

    Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...

  4. LeetCode(16)3Sum Closest

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. LeetCode(15): 每k个一组翻转链表

    hard! 题目描述: 给出一个链表,每 k 个节点为一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. ...

  6. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  7. 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  8. Web 在线文件管理器学习笔记与总结(15)剪切文件夹 (16)删除文件夹

    (15)剪切文件夹 ① 通过rename($oldname,$newname) 函数实现剪切文件夹的操作 ② 需要检测目标文件夹是否存在,如果存在还要检测目标目录中是否存在同名文件夹,如果不存在则剪切 ...

  9. Maven学习系列二(1-5)

    Maven学习系列二(1-5) 本文转自 QuantSeven 博客,讲解精炼易懂,适合入门,链接及截图如下 http://www.cnblogs.com/quanyongan/category/47 ...

随机推荐

  1. OpenCV认识之发展历史

    OpenCV认识之发展历史刚接触OpenCV,据说是专门用于机器视觉,机器人图形图像处理等,但对OpenCV的历史还不是特别了解,今天上网查好多资料,记录在此,OpenCV的全称是:Open Sour ...

  2. 接口测试postman和Jmeter

    接口测试属于功能测试的范畴,一般来说分为两种,一种是程序内部的接口,一种是系统对外的接口. postman测试接口 1. get请求: (1) 直接在浏览器中输入接口地址+?+参数 (2)使用post ...

  3. LightOj 1088 - Points in Segments (二分枚举)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...

  4. BZOJ4974(给Next求最小字典序原串)

    输入给出了最小循环节长度,暗示next数组. 然后自己按照自己的kmp板子逆着来一遍就好. ; int n, a, Next[maxn]; char str[maxn]; ]; int main() ...

  5. 题解报告:hdu1231最大连续子序列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK } ...

  6. 谷歌编码风格内容,新建一个xml文件,复制进去就可以在eclipse里面用了,命名--eclipse-java-google-style.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...

  7. 前端编辑神器---sublime text2

    个人印象 之前一直在用dreamweaver,但是由于软件太大,加载速度慢,所以sublime text2作为一款跨平台的编辑器,它的优势就展现出来了,它本身小巧,支持代码高亮,语法提示,自动完成,自 ...

  8. JavaScript创建对象的七种方法

    一. 工厂模式 创建: function createPerson(name,behavior){ var p=new Object(); p.name=name; p.behavior=behavi ...

  9. The lion king 经典句型摘录

    What am I going to do with him? Everything the light touches is our kingdom. But I thought a king ca ...

  10. Java Web MVC实例

    开发环境 Eclipse Java EE IDE for Web Developers. Version: Luna Service Release 2 (4.4.2) tomcat:7.0 数据库: ...