LeetCode OJ 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: 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]
]
解答
看这个题目,首先想到的是暴力枚举,不过做完之后看了网上的一些解答,好像暴力枚举确实是可以的。
但是作为一个可爱的娃的爸爸,我肯定是先想到排序,这样就算是暴力枚举,应该也会方便一丢丢?
不管怎么样先做个排序,排序完之后就要冷静的想一想这个问题的本质,毕竟除了3sum,这个题还可以出成4sum,5sum什么的。
我们知道,如果把这个题目改成求2sum,那么就很简单了,从头开始遍历,先确定第一个数,确定了第一个数后从尾开始遍历,不断尝试第二个数。
那么我们就可以递归的来思考这个问题,我们可以把这个问题看成,找到所有由一个数和一个2sum构成的组合,这样就很简单了。
于是我们同样从头开始遍历,先确定第一个数a,然后问题就变成,在a后面的数组中,求2sum,且2sum的和为-a。
下面是AC的代码,可能我细节方面做的还不够好,我的这个解法只能击败80%的答题者:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int i, j, k, m;
int length = nums.size();
int temp;
vector<vector<int>> ans;
if(length == 0){
return ans;
}
sort(nums.begin(), nums.end());
if(nums.front() > 0 || nums.back() < 0){
return ans;
}
for(i = 0; i < length - 2; i++){
if(i > 0 && nums[i - 1] == nums[i]){
continue;
}
for(j = i + 1, k = length - 1; j < k; ){
if(nums[i] + nums[j] + nums[k] == 0){
vector<int> temp;
temp.push_back(nums[i]);
temp.push_back(nums[j]);
temp.push_back(nums[k]);
ans.push_back(temp);
j++;
while(j < k && nums[j] == nums[j - 1]){
j++;
}
k--;
while(j < k && nums[k] == nums[k + 1]){
k--;
}
}
else if(nums[i] + nums[j] + nums[k] < 0){
j++;
}
else{
k--;
}
}
}
return ans;
}
};
101
LeetCode OJ 15. 3Sum的更多相关文章
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【一天一道LeetCode】#15 3Sum
一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- 《LeetBook》leetcode题解(15):3Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:3Sum Closest(最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Leetcode Array 15 3sum
思考的方向不对,即使用了多于别人几倍的时间,也不一定能够达到终点. 我的错误的想法(可以跳过):在leetcode上面做的第四道题,走路一个很大的弯路,收到之前做过的 Container With ...
- 【LeetCode】15. 3Sum 三个数和为0
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 【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 ...
- [LeetCode]题15:3Sum
第一次解: res = [] nums.sort() if len(nums)<3:return [] for i in range(len(nums)-2): left = i+1 right ...
随机推荐
- xdcms_3.0.1 | 代码审计
这周的审计任务,这次审计 xdcms . 下面就开始审计之旅. ...
- 贪吃蛇 v1.01
1.长度二节:2.如果触及屏幕边缘,游戏结束: 感谢张瑞阳同学改进 #include<bits/stdc++.h>#include<windows.h>#include< ...
- 如何查看yum 安装的软件路径
1.首先安装一个redis [root@iZbp1eem925ojwyx17ao9kZ ~]# yum install redis 2.查找redis的安装包 [root@iZbp1eem925ojw ...
- 牛客网剑指Offer——正则表达式匹配
1. 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整 ...
- iOS NSUserDefaults
一.介绍 NSUserDefaults适合存储请练级的本地数据,对于一些简单的数据(NSString类型)来说是首选,但是如果我们自定义了一个对象,对象保存的是一些信息,这是就不能直接存储到NSUse ...
- AWS之SSH登录:使用 PuTTY 从 Windows 连接到 Linux 实例
使用 PuTTY 从 Windows 连接到 Linux 实例 启动您的实例之后,您可以连接到该实例,然后像使用您面前的计算机一样来使用它. 注意 启动实例后,需要几分钟准备好实例,以便您能连接到实例 ...
- 【Unix网络编程】chapter8基本UDP套接字编程
chapter8基本UDP套接字编程 8.1 概述 典型的UDP客户端/服务端的函数调用 8.2 recvfrom和sendto函数 #include <sys/socket.h> ssi ...
- Vue3.0项目快速搭建
安装安装vue-cli npm install -g @vue/cli # 或者 yarn global add @vue/cli 创建项目 vue create hello-world 至此项目搭建 ...
- tomcat安全加固和规范
tomcat是一个开源Web服务器,基于Tomcat的Web运行效率高,可以在一般的硬件平台上流畅运行,因此,颇受Web站长的青睐.不过,在默认配置下其存在一定的安全隐患,可被恶意攻击.以下是一些安全 ...
- Android APP打包错误,Could not resolve com.android.tools.lint:lint-gradle:26.1.2.
1.Please select the product flavors to build and sign 不专业的翻译一下(请选择产品的味道来制作和签名)什么鬼: 选中Flavors中的选项即可: ...