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 ...
随机推荐
- 温习LOGO语言
LOGO是什么? LOGO语言是一种早期的编程语言,也是一种与自然语言非常接近的编程语言,它通过"绘图"的方式来学习编程,对初学者特别是儿童进行寓教于乐的教学方式. LOGO语言创 ...
- 如何访问google?
1:浏览器打开 uzer.me 网站(建议:火狐,不行就谷歌) 2:点击注册,注册一个账号 3:登录账号,进入如下界面,选择火狐浏览器 4:这样就可以进行国外的网站访问了
- Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
Bryce1010模板 http://codeforces.com/contest/1000/problem/E 题意: 给一个无向图,求图的最长直径. 思路:对无向图缩点以后,求图的最长直径 #in ...
- Social Net ZOJ - 3649
Social Net ZOJ - 3649 题意: 反正原题题意我是看不懂... 参考:http://www.cnblogs.com/names-yc/p/4922867.html 给出一幅图,求最大 ...
- word-wrap与word-break为长单词换行
如果你遇到长串英文单词或者url换行的问题,这时候就需要用到word-wrap与word-break这2个css属性啦. word-wrap:break-word;长单词与url地址自动换行. wor ...
- Hadoop的ChainMapper和ChainReducer使用案例(链式处理)(四)
不多说,直接上干货! Hadoop的MR作业支持链式处理,类似在一个生产牛奶的流水线上,每一个阶段都有特定的任务要处理,比如提供牛奶盒,装入牛奶,封盒,打印出厂日期,等等,通过这样进一步的分 ...
- repeater使用
Repeater: HeaderTemplate - 在加载开始执行一遍 ItemTemplate - 有多少条数据,执行多少遍 FooterTemplate - 在加载最后执行一遍 Alternat ...
- LOJ#121. 「离线可过」动态图连通性(线段树分治)
题意 板子题,题意很清楚吧.. Sol 很显然可以直接上LCT.. 但是这题允许离线,于是就有了一个非常巧妙的离线的做法,好像叫什么线段树分治?? 此题中每条边出现的位置都可以看做是一段区间. 我们用 ...
- XML验证
合法的XML和形式良好的XML ? 拥有正确语法的 XML 被称为"形式良好"的 XML. 第一行是 XML 声明.它定义 XML 的版本 (1.0) 和所使用的编码 (ISO-8 ...
- Linux 使用常见问题
1. 如何查看软件安装到什么位置 [Ubuntu] 今天安装了Lxc-docker,想看一下文件都安装到哪里了,首先找到这个包的ersion zhouh1@uhome:~$ dpkg -s lxc-d ...