荒废好久没更新了,时间过得很快,转眼就2017年了,经历了苦闷的科研阶段,发了论文顺利毕业;也经过三地辗转奔波来去的找工作,最终还是犹犹豫豫选择了自己知道以后可能会后悔的,果然就后悔了。所以还是应该选择自己喜欢的,当然也许人总是会这样,总觉得没走的另一条路也许走起来更畅通。

  

  Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

  解题思路:本题的思路与2 sum类似,首先对数组进行排序,然后依次把每位数字当做target,用类似于处理2 sum的思路进行两边扫描向中间逼近的处理。只是在此题中要注意重复的问题。

  左数从i+1开始,可以理解为处理的sum[i]作为三个数中的最左值,从此数的右边第一位和数据的最后一位向中间靠拢寻找符合的数字组合。如果把sum[i]作为中间的值来处理则存在找出重复数组的风险,如下:

input:-1、-1、0、1、2output:[-1,-1,2] [0,-1,1] [1,-1,0]....//可以看到如果允许跨过target本身去寻找,就存在重复的风险
int left = i + 1;
int right = nums.size() - 1;

因为数组已经是排过序的,因此检查如下语句,避免作为左值的数重复。

if(i > 0 && nums[i] == nums[i - 1]) //continue;

  数组中可能存在不止一组数字的和与target相加的值等于零,因此两边继续向内扫描,如下两句则是避免中间数字和右边数字重复的问题。

while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right && nums[right] == nums[right -1 ]) right--;

  

  完整代码如下:

class Solution {
public:vector<vector<int>> threeSum(vector<int>& nums) {vector<vector<int>> resl;int length = nums.size();if(length < 3)return resl;sort(nums.begin(), nums.end());for (int i = 0; i < nums.size() - 1; ++i){if(i > 0 && nums[i] == nums[i - 1])continue;int left = i + 1;int right = nums.size() - 1;while(left < right){vector<int> temp;if(nums[i] + nums[left] + nums[right] == 0){temp.push_back(nums[i]);temp.push_back(nums[left]);temp.push_back(nums[right]);while(left < right && nums[left] == nums[left + 1]) left++;while(left < right && nums[right] == nums[right -1 ]) right--;left++;right--;}else if (nums[i] + nums[left] + nums[right] < 0)++left;else --right;if(temp.size() > 0)resl.push_back(temp);}}return resl;}
};

转载于:https://www.cnblogs.com/echo-lsh/p/6496911.html

leetcode 3Sum C++相关推荐

  1. 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 ...

  2. 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 ...

  3. LeetCode -- 3Sum

    Question: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F ...

  4. LeetCode - 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. LeetCode 3sum 问题

    1. Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  6. 259 [LeetCode] 3Sum Smaller 三数之和较小值

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  7. [LeetCode] 3Sum Closest

    依旧先来题目: Given an array S of n integers, find three integers in S such that the sum is closest to a g ...

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

  9. [LeetCode]3Sum Closest

    题目 Number: 16 Difficulty: Medium Tags: Array, Two Pointers Given an array S of n integers, find thre ...

最新文章

  1. 【强势来袭】Node.js(nodejs)实现“一口多用”(含用户创建、登录、鉴权token) 一个文件解决所有常态化需求
  2. Spring Boot 面试杀手锏:自动配置原理
  3. WordPress插件开发: 文章同步到OSC博客插件(OscPress) (四)
  4. 【云栖大会】基因计算:解读生命的力量
  5. 中国人口较少民族作家研讨会
  6. 【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级 )】小乐乐和25(模拟,技巧)
  7. erp生产管理系统排名_仁和ERP软件企业管理系统如何提高生产管理
  8. ios assign、copy 、retain
  9. 高分七号卫星发射成功
  10. 机器人领域的会议和期刊【补充】
  11. java seek_java中seek()的用法
  12. Warning: continue targeting switch is equivalent to break. Did you mean to use continue 2? 故障
  13. python eml解析_如何在python中读取eml文件?
  14. VisualStudio 如何使用UML呢?(转自简书雨落随风)
  15. 数据分析与数据仓库建模
  16. mymps蚂蚁分类信息模板二次开发调用标签
  17. python excel画图_python读取excel数据并且画图
  18. 一个BUG(缺陷)的生命周期
  19. OFsuite亮相SDNFV Fest测试论坛 控制器性能测试成看点
  20. OBS无法捕捉显示屏

热门文章

  1. delphi7 提示注册过期问题
  2. Can't find temporary directory:internal error
  3. Mecanim动画系统
  4. 事实上着就是MAYA4.5完全手册插件篇的内容
  5. Nagios使用check_mysql_health插件监控Mysql主机
  6. ocp linux 基础要点
  7. 你的工作单位也需善待
  8. Xcode 的正确打开方式——Debugging
  9. 如何使用FF的Firebug组件中的net工具查看页面元素加载消耗时间
  10. apache php mysql codeigniter smarty 记录方便查询