LeetCode 3Sum (Two pointers)
题意
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.
找出一个数组中的三个数,使这三个数的和为0。输出所有的组合,不能重复。
解法
最简单的思路就是跑一个三层循环,暴力枚举所有组合,很显然会超时。
然后考虑排序后跑两层循环,第三层改用二分查找,即确定前两个数后用二分来搜第三个数,时间复杂度降到了O(logN * N^2),还是会超时。
最后,采用了Two Sum这一题的办法,遍历第一个数,然后剩下的两个数用双指针算法来找,这样时间复杂度就降到了O(N^2)
还有一个问题是判重,这里采用的办法是将三个数拼接起来成为一个数,比如【-1,0,1】就被保存成-101,用Long Long来存,然后放到一个Map里,每次选取新答案时都判断一下这样的组合是不是能在Map里找到。
class Solution
{
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
map<long long,bool> vis;
sort(nums.begin(),nums.end());
vector<vector<int>> rt;
for(int i = 0;i < nums.size();i ++)
{
if(nums[i] > 0)
break;
int j = i + 1;
int k = nums.size() - 1;
while(j < k)
{
if(nums[i] + nums[j] + nums[k] == 0)
{
long long box = abs(nums[i]); // 判重
int temp = abs(nums[j]);
while(temp)
{
box *= 10;
temp /= 10;
}
box += abs(nums[j]);
temp = abs(nums[k]);
while(temp)
{
box *= 10;
temp /= 10;
}
box += abs(nums[k]);
if(nums[i] * nums[j] * nums[k] < 0)
box = -box;
if(vis.find(box) == vis.end())
{
vis[box] = true;
rt.push_back({nums[i],nums[j],nums[k]});
}
j ++;
}
if(nums[i] + nums[j] + nums[k] < 0)
j ++;
else if(nums[i] + nums[j] + nums[k] > 0)
k --;
}
}
return rt;
}
};
LeetCode 3Sum (Two pointers)的更多相关文章
- LeetCode 3Sum Closest (Two pointers)
题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 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] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- leetcode — 3sum
import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...
- LeetCode 4Sum (Two pointers)
题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- LeetCode: 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
随机推荐
- ecplice 中添加JavaFX插件学习
fxml文件使用SceneBuilder打开报错 解决方法:Window-->Preferences-->JavaFX-->browse 路径是可执行的JavaFX Scene Bu ...
- jqery-easyui的Datagrid的介绍-Pagination事件
Datagrid(数据表) 依赖的组件 resizable linkbutton pagination DataGrid Options对象的属性 名称(Name) 类型(Type) 描述(Descr ...
- Oracle 导出用户下的所有索引创建语句
SELECT dbms_lob.substr(dbms_metadata.get_ddl('INDEX', INDEX_NAME))||';' from dba_indexes where owne ...
- October 11th 2017 Week 41st Wednesday
If you don't know where you are going, you might not get there. 如果你不知道自己要去哪里,你可能永远到不了那里. The reward ...
- easyui学习笔记14-拓展的基本验证规则
/** * 扩展的基本校验规则, */ $.extend($.fn.validatebox.defaults.rules, { minLength : { // 判断最小长度 validator : ...
- [A]1065 A+B and C (64bit)(挖坑待填)
Given three integers A, B and C in [-2^63, 2^63], you are supposed to tell whether A+B > C. Input ...
- 旋转的地球css3
css3果然博大精深: 1.代码里面还有用box-shadow制作椭圆形阴影的效果,厉害了!之前找了好久都没找到,今天给找到了 html: <section class="stage& ...
- python difflib.md
difflib 此模块提供了用于比较序列的类和函数.它可以用于例如比较文件,并且可以产生各种格式的差异信息,包括HTML和上下文以及统一差异. difflib 模块包含用于计算和处理序列间差异的工具. ...
- Android与js交互拍照上传资料
应用场景:h5通知android端拍照,选相册,然后将图片路径上传成功之后,获取到网络路径,将此路径返还给h5界面,并展示出来. android与js快速交互 效果图如下: 1.在Activity ...
- Spring AOP示例代码
public interface CustomerDao { public void save(); public void update(); } public class CustomerDaoI ...