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.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • 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)

解题思路o(n2):

先对数组进行排序,排序后以每一个数组元素为目标值,在其他的元素中寻找余下的两个元素,使这三个元素之和为0;那么此题就转化为leetcode中另一道TwoSum的问题,由于3Sum已经提前排序,因此TwoSum中的hash表技巧在这种思路下就没有时间复杂度优势了。

注意:

1、不需要遍历所有数组元素,只需要将所有非正元素,或者所有非负元素依次作为目标值即可;

2、在相同目标值的前提下,TwoSum可能有多种匹配对(当然);

3、此题最重要的是避免返回的答案序列出现重复,重复有两种情况:

  (1)TwoSum的计算结果出现重复。

    解决方法:以i和j从序列两头向中间遍历,如果遇到符合条件的i和j,则跨越所有值相同的i和j,再继续遍历之后的数字;

  (2)目标值出现重复。

    解决方法:a. 从左向右遍历,只遍历非正数;(或者从右向左遍历,只遍历非负数)

         b. 跨过所有相同的重复数字;

    重复去除的关键在于,第一次出现重复数字时,其数字选择完全包含了之后重复数字的选择,那么之后遇到重复数字直接略过就行了。

解题步骤:

1、复制输入数组,并对复制数组进行排序;

2、新建一个保存结果的二维数组;

3、从0开始遍历,直到复制数组元素<=0:

  (1)将遍历到的元素下标,随同复制数组以及保存结果的二维数组,传入FindTwoSum函数中。

  (2)略过和遍历到的元素相同的元素;

4、返回结果数组;

对于FindTwoSum:

1、从遍历元素下标+1,到数组末尾,两端向中间遍历此数组:

  (1)判断两端及遍历元素,三者之和是否为0;进而做++i和++j的处理;

  (2)三者和为0,则这三者放入结果数组中,并且略过重读的num[++i]和num[--j];

AC代码:

 class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(), num.end());
int size = num.size();
vector<vector<int> > result; for (int i = ; i < size - && num[i] <= ; ++i) {
FindTwoSum(result, num, i);
while (num[i] == num[i+])
++i;
}
return result;
} void FindTwoSum(vector<vector<int> > &result, vector<int> num, int target_idx) {
int target = num[target_idx];
int i = target_idx + ;
int j = num.size() - ;
while (i < j) {
if (num[i] + num[j] + target > ) {
--j;
} else if (num[i] + num[j] + target < ) {
++i;
} else {
vector<int> oneMatch {target, num[i], num[j]};
result.push_back(oneMatch);
while (num[i] == num[++i]);
while (num[j] == num[--j]);
}
}
return;
}
};

【Leetcode】【Medium】3Sum的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  6. 【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 ...

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

  8. 【LeetCode从零单排】No15 3Sum

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

  9. 【LeetCode每天一题】3Sum Closest(最接近的三数和)

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  10. 【LeetCode每天一题】3Sum(三数之和)

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

随机推荐

  1. 1 复习ha相关 + weekend110的hive的元数据库mysql方式安装配置(完全正确配法)(CentOS版本)(包含卸载系统自带的MySQL)

    本博文的主要内容是: .复习HA相关 .MySQL数据库 .先在MySQL数据库中建立hive数据库 .hive的配置 以下是Apache Hadoop HA的总结.分为hdfs HA和yarn HA ...

  2. Software Architecture Pattern(Mark Richards)笔记

    软件架构模式 缺少规范架构的程序通常会变得紧耦合.脆弱.难以更改,缺少清晰的发展方向和愿景.这本小书用50多页介绍了常用的5种常见架构模式,相信不管是大牛还是萌新都会有所收获,特别是对我这种偏爱系统设 ...

  3. JVM Run-Time Data Areas--reference

    http://www.programcreek.com/2013/04/jvm-run-time-data-areas/ This is my note of reading JVM specific ...

  4. [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法

    问题 怎样找出一个序列中出现次数最多的元素呢? 解决方案 collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案 c ...

  5. master.sys.sysprocesses相关内容

    sysprocesses 表中保存关于运行在 Microsoft® SQL Server™ 上的进程的信息.这些进程可以是客户端进程或系统进程. sysprocesses 只存储在 master 数据 ...

  6. html空白文字宽度

    原文链接 名称 编号 描述     不断行的空白(1个字符宽度)     半个空白(1个字符宽度)     一个空白(2个字符宽度)     窄空白(小于1个字符宽度) 小写加分号!

  7. webservice log4net日志写入失败

    原因1:如果webservice和调用者都部署在一台机器上,日志有可能写到了项目所在目录中,虽然你添加的服务引用是部署在iis下的,但不会写到这.暂时解决办法,把webservice部署到内网服务器上 ...

  8. php 正则验证

      PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判 ...

  9. Django 模型层之单表操作

    一.单表操作之创建表 在app的models.py文件中创建模型: from django.db import models class Book(models.Model): id = models ...

  10. BBS需求分析和orm设计

    一.BBS博客需求分析 首页(现实文章) 文章详情 点赞 文章评论(子评论,评论的展示) 登录功能(图片验证码) 注册功能(基于form验证) 个人站点(不同人不同样式,文章过滤) 后台管理(文章展示 ...