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 ...
随机推荐
- Twenty score
1.上图中有两个人对读书的看法有较大的不同. There are two people in the cartoon who treat books in completely different w ...
- 【Hadoop 分布式部署 六:环境问题解决和集群基准测试】
环境问题: 出现Temporary failure in name resolutionp-senior-zuoyan.com 的原因有很多,主要就是主机没有解析到, 那就在hadoop的sl ...
- Google advertiser api开发概述——最佳做法&建议
最佳做法 本指南介绍了一些最佳做法,您可以运用它们来优化 AdWords API 应用的效率和性能. 日常维护 为确保您的应用不间断运行,可采取以下做法: 确保 AdWords API 中心中的开发者 ...
- 2、corosync集群初步
配置高可用集群 配置环境:两台centos7 192.168.184.141 192.168.184.142 corosync v2 + pacemaker corosync v2:vote sys ...
- 2、My Scripts
http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html 注释详解 1.打印选择菜单,按照选择项一键安装不同的web服 ...
- webservice跨域问题
在webconfig里面加上 <httpProtocol> <customHeaders> <add name="Access-Co ...
- 大整数加法 HDU1002
今天早上没事干又把这个敲了一遍,虽然手冻得不行,不过又深入理解理解还可以哈. 难点就在给你的整数可能很大很长,所以long long 肯定不行,得用字符串来读取存储,然后注意一下相加的时候进位,最后输 ...
- Math.abs(~2020) 按位取反后的绝对值是多少 2021, 按位取反后,比正数多1
Math.abs(~2020) 按位取反后的绝对值是多少 2021, 按位取反后,比正数多1 int 值的取值 范围: -128 --- 127 之间, 0000 0000 按位取 ...
- input 输入值的监听 禁止输入特殊字符
1.input 输入值的监听 //用于监听input的值变化(input的值产生变化才会触发事件) (function ($) { $.fn.watch = function (callback) ...
- myeclipse新建maven项目
右键 new--->project ---->maven project 出现bank 右键点击bank---->maven4Myeclipse---->new maven ...