Leetcode(15)-三数之和
给定一个包含 n 个整数的数组 nums
,判断 nums
中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
自己的思路:如果追求思路简单,就是暴力的去求解每三个字符的和是否为0,但是有三层循环,时间复杂度太高,提交,超出时间限制。
(1)
- 排序。排序可以让数据变得有序,在许多算法中都有使用。
- 定义三个指针
i,left,right
. - i用来遍历数据,从下下标为0一直到len-3。
- 对于每一个i,令
left=i+1,right=len-1
,然后检查三个指针所指数据的和,
如果为0,说明找到一个符合条件的组合,把三个数放入vector中然后再放入map中,接着把left++,right--。
如果>0,则说明当前的right偏大,则把right--,因为是排序的,所以整体和会变小。
如果<0,说明当前的left偏小,则把left++,因为是排序的,所以整体的和会变大。(这里变大变小不是一定的,因为可能存在重复的数据,不影响)vector<vector<int>> threeSum(vector<int>& nums)
{ vector<vector<int>> result;
int len = nums.size();
map<vector<int>,int> m;
if(nums.empty()||len<3) return result;
sort(nums.begin(),nums.end());
vector<int> res;
int p1,p2,p3;
int i=0;
for(p1=0;p1<=len-3;p1++)
{
int left=p1+1;
int right = len-1;
while(left<right)
{
if(nums[p1]+nums[left]+nums[right]==0)
{
res.push_back(nums[p1]);
res.push_back(nums[left]);
res.push_back(nums[right]);
if(!m.count(res))
{
m.insert(map<vector<int>,int>::value_type(res,i++));
result.push_back(res);
}
res.clear();
left++;
right--;
}
else if(nums[p1]+nums[left]+nums[right]<0)
left++;
else
right--;
}
}
return result;
}这里用map和map中的count函数是为了去除相同的组合,因为数组是排序好的,所以找到的组合也是排序好的,如果相同的话,是完全相同的,否则我们还得先排序才能比较。
(2)大神们还有比这个更加快速的解决方案,那就是
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> rs;
vector<int> res;
if(nums.size() == 0) return rs;
sort(nums.begin(), nums.end());//先对目标数组进行从小到大的排序
for (int i = 0; i < nums.size() - 2; i++)
{
if (nums[i] > 0) return rs;//如果最小值都大于0,那么就不用再找了
if (i > 0 && nums[i] == nums[i-1]) //如果和前一个值相等,就跳过,去重
continue;
int tmpTarget = 0 - nums[i];//接下来就求nums[i]的相反数 int beg = i + 1, end = nums.size() - 1;//也是从两边开始往中间查找
while (beg < end)
{
if (nums[beg] + nums[end] == tmpTarget)
{
res.push_back(nums[i]);
res.push_back(nums[beg]);
res.push_back(nums[end]);
rs.push_back(res);
while(beg < end - 1 && nums[end] == nums[end-1]) //同样是和前一个相同,去重
end--;
end--;
res.clear();
}
else if (nums[beg] + nums[end] > tmpTarget)
end--;
else
beg++;
}
}
return rs;
}
Leetcode(15)-三数之和的更多相关文章
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 15. 三数之和(3Sum)
题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- [Leetcode 15]三数之和 3 Sum
[题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...
- [LeetCode]15. 三数之和(数组)(双指针)
题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三 ...
- [LeetCode] 15. 三数之和
题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...
- LeetCode:三数之和【15】
LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- leetcode题目15.三数之和(中等)
题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
随机推荐
- Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节
目录 Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节 优先队列的默认用法-从小到大排序 对String类用优先队列从大到小排序 通过自定义比较器对自定义的类进行从小到大 ...
- 在.NET Core 中实现健康检查
.NET Core中提供了开箱即用的运行状况检查,首先,我将在.NET Core API应用程序中执行运行状况检查,接下来,我们将使用DbContext集成SQL Server或数据库的运行状况检查, ...
- Boyer-Moore 投票算法
Boyer-Moore 投票算法 http://theory.stanford.edu/~trevisan/cs154-12/notestream.pdf 众数
- How does Go kit compare to Micro?
Go kit - Frequently asked questions https://gokit.io/faq/ How does Go kit compare to Micro? Like Go ...
- python 使用函数名的字符串调用函数(4种方法)_black-heart的专栏-CSDN博客 https://blog.csdn.net/mrqingyu/article/details/84403924
funcs = ['fetch_data_' + i for i in ( 'activities', 'banners', 'server_list')]# from operator import ...
- 命名规范 api-guidelines api规范
https://weui.io weui.css .weui-cell_select-before .weui-cell__bd:after{ display:none; } .weui-cell_s ...
- JAD 反编译
自动拆装箱 对于基本类型和包装类型之间的转换,通过xxxValue()和valueOf()两个方法完成自动拆装箱,使用jad进行反编译可以看到该过程: public class Demo { publ ...
- Spring Cloud系列之Commons - 1. 背景与基础知识准备
本文基于 Spring Cloud 2020.0 发布版的依赖 本系列会深入分析 Spring Cloud 的每一个组件,从Spring Cloud Commons这个 Spring Cloud 所有 ...
- python 文件的方法
1.open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open ...
- 二:整合Spring Security
整合Spring Security 1.项目创建 2.初次体验 3.用户名配置 3.1 配置文件配置用户名/密码 3.2 Java 配置用户名/密码 4.登录配置 5.忽略拦截 江南一点雨:Sprin ...