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 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;
}
};
LeetCode(15) 3Sum的更多相关文章
- LeetCode(15)3Sum
题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...
- Leetcode(15)-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode(15):三数之和
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...
- 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 ...
- LeetCode(15): 每k个一组翻转链表
hard! 题目描述: 给出一个链表,每 k 个节点为一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. ...
- 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 ...
- 理解 neutron(15):Neutron linux-bridge-agent 创建 linux bridge 的简要过程
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- Web 在线文件管理器学习笔记与总结(15)剪切文件夹 (16)删除文件夹
(15)剪切文件夹 ① 通过rename($oldname,$newname) 函数实现剪切文件夹的操作 ② 需要检测目标文件夹是否存在,如果存在还要检测目标目录中是否存在同名文件夹,如果不存在则剪切 ...
- Maven学习系列二(1-5)
Maven学习系列二(1-5) 本文转自 QuantSeven 博客,讲解精炼易懂,适合入门,链接及截图如下 http://www.cnblogs.com/quanyongan/category/47 ...
随机推荐
- Nginx的location配置概述【转】
语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配url路径即可.nginx不对url做编码,因此请 ...
- macOS 设置Root密码
用管理员帐号进入Terminal: 1) 输入:sudo passwd root ,回车: 2) 输入新的root密码: 3) 输入:su : 4) 输入新密码: 这样就进入到root帐号了.
- 跟我一起玩Win32开发(23):渐变颜色填充
GradientFill函数可以对特定的矩形区域或者三角形区域进行渐变颜色的填充.我们先来看看GradientFill函数到底长得什么样子,帅不帅. BOOL GradientFill( _In_ ...
- Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)
题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...
- Graph HDU - 4467
https://vjudge.net/problem/HDU-4467 大概就是,设一个块大小T 对于度数<=T的点,设为1类点,在改变颜色的时候暴力查询与其相邻点,更新答案 对于度数>T ...
- EXBSGS
http://210.33.19.103/problem/2183 参考:https://blog.csdn.net/frods/article/details/67639410(里面代码好像不太对) ...
- Monitor CodeForces - 846D
题目 题意:有一个n*m的显示屏,有q个坏点先后出现,已知第i个坏点位置为(xi,yi),在ti时间出现.显示屏上出现一个k*k的矩阵全都是坏点时显示屏就是坏的.输出显示屏坏的时间,如果不会坏就输出- ...
- Linux Ubuntu 14.04 LTS下VirtualBox连接USB
1.环境 主机:Ubuntu 14.04 LTS 虚拟机:Windows 7 专业版本 VirtualBox: 图形用户界面版本 5.1.8 r111374 (Qt5.6.1) 2.在主机上给Virt ...
- FACVSPOW - Factorial vs Power 数学方法 + 二分
http://www.spoj.com/problems/FACVSPOW/ 求解n! > a^n最小的整数n 对于有n!和a^n的东西,一般是取ln 然后就是求解 (ln(1) + ln(2) ...
- vscode中将本地数据push至git repository
1.新建repository 2.本地写好的代码 3.执行git init 初始化git配置文件 4.提交已暂存文件 5.填写提交信息 6.执行push命令 7.完成