参考资料:

https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N)-with-explanation-and-comments

https://www.cnblogs.com/grandyang/p/4481576.html

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int> > res; std::sort(nums.begin(), nums.end()); for (int i = ; i < nums.size(); i++) { int target = -nums[i];
int front = i + ;
int back = nums.size() - ; while (front < back) { int sum = nums[front] + nums[back]; // Finding answer which start from numsber nums[i]
if (sum < target)
front++; else if (sum > target)
back--; else {
vector<int> triplet(, );
triplet[] = nums[i];
triplet[] = nums[front];
triplet[] = nums[back];
res.push_back(triplet); // Processing duplicates of numsber 2
// Rolling the front pointer to the next different numsber forwards
while (front < back && nums[front] == triplet[]) front++; // Processing duplicates of numsber 3
// Rolling the back pointer to the next different numsber backwards
while (front < back && nums[back] == triplet[]) back--;
} } // Processing duplicates of numsber 1
while (i + < nums.size() && nums[i + ] == nums[i])
i++; } return res;
}
};

15. 3Sum C++的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  3. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  4. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  5. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  6. 刷题15. 3Sum

    一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...

  7. [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 un ...

  8. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

随机推荐

  1. Latex: IEEEtrans模板下 扩大标题宽度

    参考: Extending side margins for Title section in IEEEtrans document class Latex: IEEEtrans模板下 扩大标题宽度 ...

  2. 所有JTAG集成电路都应该支持菊花链

    菊花链 在电气和电子工程中,菊花链是一种布线方案,其中多个设备按顺序或环形连接在一起.相邻设备才能通信.菊花链可用于电源,模拟信号,数字数据或其组合. 但是由于菊花链的串联特性,如果任何一个设备从链路 ...

  3. PHP中SESSION自定义会话管理器

    <?php class CustomSession implements SessionHandlerInterface{ private $link; private $lifetime; p ...

  4. 键盘控制div移动并且解决停顿问题(原生js)

    <html> <head> <title>键盘控制div移动,解决停顿问题</title> <meta charset="utf-8&q ...

  5. vue中click阻止事件冒泡,防止触发另一个事件

    在使用el-upload组件时,在其中放置了一个删除按钮的图片. 当点击图片,本想只删除上传的视频,但是意外触发了el-upload中的事件 解决办法:用stop,结果只删除当前预览,不触发上传事件. ...

  6. SpringLog4j日志体系实现方式

    1.通过web.xml读取log4j配置文件内容 2.通过不同的配置信息,来实现不同的业务输出,注意:log4j可以写入tomcat容器,也可以写入缓存,通过第三方平台读取 #输入规则#log4j.r ...

  7. echo -n 和echo -e 参数意义

    echo -n 不换行输出 $echo -n "123" $echo "456" 1 2 最终输出 123456 而不是 123 456 1 2 3 4 5 6 ...

  8. 副本死亡传送(_instance_die_tele)

    玩家在副本中死亡时,将传送至对应的坐标 mapId 副本地图ID posId 坐标ID,对应_postion中posId

  9. springmvc处理过程理解(一)

    DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器:根据url查找Handler.(可以通过xml配置方式,注解方式) H ...

  10. MySQL数据分组Group By 和 Having

    现有以下的学生信息表: 若果现在想计算每个班的平均年龄,使用where的操作如下: SELECT Cno AS 班级, AVG(Sage) AS 平均年龄 FROM stu ; 这样的话,有多少个班就 ...