leetcode-algorithms-15 3Sum
leetcode-algorithms-15 3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
解法
对于a+b+c=0,只要求b+c=-a.定义第一个数为-a.其它两数相加和为a就是要的结果.
class Solution
{
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
std::sort(nums.begin(), nums.end());
std::vector<vector<int> > res;
int len = nums.size();
for (int i = 0; i < len; ++i)
{
int target = -nums[i];
int front = i + 1;
int back = len - 1;
while(front < back)
{
int sum = nums[front] + nums[back];
if (target < sum)
--back;
else if (target > sum)
++front;
else
{
std::vector<int> v(3);
v[0] = nums[i];
v[1] = nums[front];
v[2] = nums[back];
res.push_back(v);
//去掉前置重复
while (front < back && nums[front] == v[1]) ++front;
//去掉后置重复
while (front < back && nums[back] == v[2]) --back;
}
}
//去掉target重复
while(nums[i + 1] == nums[i]) ++i;
}
return res;
}
};
时间复杂度: O(n^2).
空间复杂度: O(1).
leetcode-algorithms-15 3Sum的更多相关文章
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【一天一道LeetCode】#15 3Sum
一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- LeetCode OJ 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 ...
- 《LeetBook》leetcode题解(15):3Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Leetcode Array 15 3sum
思考的方向不对,即使用了多于别人几倍的时间,也不一定能够达到终点. 我的错误的想法(可以跳过):在leetcode上面做的第四道题,走路一个很大的弯路,收到之前做过的 Container With ...
- 【LeetCode】15. 3Sum 三个数和为0
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 【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 ...
- [LeetCode]题15:3Sum
第一次解: res = [] nums.sort() if len(nums)<3:return [] for i in range(len(nums)-2): left = i+1 right ...
- [LeetCode][Python]15:3Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...
随机推荐
- 论文笔记之:Action-Decision Networks for Visual Tracking with Deep Reinforcement Learning
论文笔记之:Action-Decision Networks for Visual Tracking with Deep Reinforcement Learning 2017-06-06 21: ...
- 移动端开发:使用jQuery Mobile还是Zepto
原:http://blog.csdn.net/liubinwyzbt/article/details/51446771 jQuery Mobile和Zepto是移动端的js库.jQuery Mobil ...
- 在centos6中编译安装httpd-2.4/搭建LAMP
首先确保centos6安装平台安装一下环境: #yum groupinstall "Development Tools" "Server Platform Develo ...
- Js 运行机制 (重点!!)
一.引子 本文介绍JavaScript运行机制,这一部分比较抽象,我们先从一道面试题入手: 这一题看似很简单,但如果你不了解JavaScript运行机制,很容易就答错了.题目的答案是依次输出1 2 3 ...
- Git、GitHub、GitLab三者之间的联系以及区别
在讲区别以及联系之前先简要的介绍一下,这三者都是什么(本篇文章适合刚入门的新手,大佬请出门左转) 1.什么是 Git? Git 是一个版本控制系统. 版本控制是一种用于记录一个或多个文件内容变化,方便 ...
- C++使用thread类进行多线程编程
C++11中引入了一个用于多线程操作的thread类,简单多线程示例: #include <iostream> #include <thread> #include <W ...
- react-redux的mapStateToProps可取到state值但不会注入props
一.问题描述 想通过react-redux和redux实现react组件之间的通信,reducer.action.store都编写正确,mapDispatchToProps也能正确传值.唯独mapSt ...
- 无损录制acestream直播流
很多人选择用acestream收看一些频道,往往比较高清,但很多人不知道如何录制. 其实方法很简单,用ace自带的播放器播放后: 1.打开potplayer,右击->打开链接 2.把当前播放的a ...
- Codeforces 786 C. Till I Collapse
题目链接:http://codeforces.com/contest/786/problem/C 大力膜了一发杜教的代码感觉十分的兹瓷啊! 我们知道如果$k$是给定的我们显然是可以直接一遍$O(n)$ ...
- Q-Q图
来自:https://mp.weixin.qq.com/s/_UTKNcOgKQcCogk2C2tsQQ 正负样本数据集符合独立同分布是构建机器学习模型的前提,从概率的角度分析,样本数据独立同分布是正 ...