【Leetcode】【Medium】3Sum
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.
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【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从零单排】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 ...
- 【LeetCode每天一题】3Sum Closest(最接近的三数和)
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- 【LeetCode每天一题】3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
随机推荐
- xcode配置文件中,Architectures表示的意义
xcode配置文件中,Architectures表示的意义 ======================== Architecture : 指你想支持的指令集. Valid architectures ...
- Centos7 安装mysql教程
参考原文:http://www.centoscn.com/mysql/2016/0315/6844.html 环境 CentOS 7.1 (64-bit system) MySQL 5.6.24 Ce ...
- 【vm安装vmtools】
使用sudo ./安装命令 对vmware-tools-distrib文件夹里面vmware-install.pl文件夹进行安装 sudo ./vmware-install.pl
- Firebird Connection pool is full
今天在做Firebird V3.0.3 x64 版本内存测试,本地PC上,准备开启800个事务(保持不关闭),每个事务做些事,尽量不释放内存. 每次测试当事务数达到时,就提示Connection p ...
- requirejs和seajs使用感受
这几天看了下前端模块化的知识,主要是requirejs和seajs相关的知识,还未看es6的模块化知识. 由于目前项目组内的开始推广使用vue,并且开始简单的封装组件,但发现组件js的使用方式依然是原 ...
- c#中用DataView查询
新人菜鸟 在开发一款软件,用到了 MyS中的 DataView 查询.查询后判断视图是否有返回值,找了好久,终于找到了他的 DataView XX.Count 参数. string table = S ...
- TortoiseGit记住用户名&密码
配置并安装好git之后鼠标右键: 在全局配置文件末尾添加一行: [credential] helper = store *主意保存时以utf-8格式保存,否则中文可能会乱码,这样下次只需输入一次用户名 ...
- CXF生成客户端遇到的问题
一.CXF环境配置路径错误 1.错误现象 在命令行中输入 wsdl2java -v 检查CXF安装是否正确. 出现错误=> ERROR: Unable to find cxf-manifest. ...
- Tomcat源码(二):tomcat启动之前的初始化
当tomcat启动的时候 首先会加载 org.apache.ctalina.startup.BootStrap类. 使用eclipse或idea启动tomcat其实就是在启动这个类的main方法 根据 ...
- 一、IOC和DI的概念
IOC---Inversion of Control (控制反转) 在java中,IOC意味着将你设计好的对象交给容器控制,而不是传统的在你对象内部直接控制. 谁控制谁,控制什么 -->IOC ...