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 ...
随机推荐
- hdu1233 还是畅通工程 基础最小生成树
//克鲁斯卡尔 #include<iostream> #include<algorithm> using namespace std; ; struct node { int ...
- 如何在Windows2008 Server服务器上开启Ping或者禁PING
方法1:命令行模式 进入服务器后 点击 开始--运行 输入命令: netsh firewall set icmpsetting 8 这样就可以在外部ping到服务器了 非常简单实用! 同样道理,如果想 ...
- mvc报( 检测到有潜在危险的 request.form 值 )错的解决方案
今天在做项目中遇到了报( 检测到有潜在危险的 request.form 值 )错,百度过后解决了该问题,出此问题主要还是因为提交的Form中有HTML字符串,例如你在TextBox中输入了html标签 ...
- 【转】数据库CRUD操作
数据库CRUD操作 一.删除表 drop table 表名称 二.修改表 alter table 表名称 add 列名 数据类型 (add表示添加一列) alter table 表名 ...
- 【学习笔记】深入理解js原型和闭包(6)——继承
为何用“继承”为标题,而不用“原型链”? 原型链如果解释清楚了很容易理解,不会与常用的java/C#产生混淆.而“继承”确实常用面向对象语言中最基本的概念,但是java中的继承与javascript中 ...
- 【学习笔记】Base64编码解码原理及手动实现(C#)
1.[Base64编码原理]@叶落为重生 -base64的编码都是按字符串长度,以每3个8bit的字符为一组,-然后针对每组,首先获取每个字符的ASCII编码,-然后将ASCII编码转换成8bit的二 ...
- 类成员的指针必须NULL化,否则是乱七八糟的东西
class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...
- idea 下maven 导入本地jar,以及导入之后 java不能引用问题
1.在当前的项目中新建立一个lib文件夹,将需要导入的jar放入其中. 2.配置pom.xml 文件 <!--导入本地jar--> <dependency> <group ...
- How to proxy a web site by apache2 in Ubuntu
Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...
- subprocess使用小方法
import subprocess def create_process(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subp ...