LeetCode OJ 3Sum 3个整数之和
题意:给一个vector<int>容器,要求每当找到3个元素之和为0时就将这3个数按大小顺序记下来,用一个二维vector返回。也就是vector< vector<int> >类型的。
思路:2sum是用的两个指针,那么3sum就可以利用2sum的思路解决,假如先挑一个元素出来,则还需挑2个元素,就可以用2sum的思路了。首先将n个元素排序先。怎么挑第1个元素?一共有n种选择,但是为了防止重复,后2个不用挑,留给两个指针来解决,也就是有n-2个元素。而两个指针的可以挑的有哪些呢?假设第1个元素是第i个,那么这两个指针应该在第i个之后的元素挑,为什么呢?
比如数组为:-2 -1 0 1 2
第一轮循环:
第一个元素:-2
第二个元素:0
第三个元素:2
第2轮循环应该是:
第一个元素:-1
第二个元素:0 (难道还想试试-2? -2可以搭配的所有可能都已经试出来了,这里不用再重试了)
第三个元素:1
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(), num.end()); //排序
vector<int> group(,);
vector< vector<int> > ans;
int n=num.size(),sum,sum2,*pl,*pr,old=;
for(int i=; i<n-; i++ )
{
if( num[i] == old ) continue;//去重,首数字不能重复
else old = num[i];
sum2 = - num[i];//寻找余下两数之和
pl = &num[i+];//左指针
pr = &num[n-];//右指针
while(pl!=pr)
{
sum = *pl + *pr;
if( sum == sum2 )
{
if( group[]!=num[i] || group[]!=*pl || group[]!=*pr)//去重,防止找到的和上一次刚好一样
{
group[] = num[i];
group[] = *pl;
group[] = *pr;
ans.push_back(group);
}
pl++;
}
else if( sum > sum2 ) pr--;
else pl++;
}
}
return ans;
}
};
3Sum
LeetCode OJ 3Sum 3个整数之和的更多相关文章
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 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 all un ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- leetcode python两整数之和
# Leetcode 371 两整数之和***### 题目描述 **不使用**运算符 `+` 和 `-` ,计算两整数 `a `.`b` 之和. **示例1: ...
- Java实现 LeetCode 371 两整数之和
371. 两整数之和 不使用运算符 + 和 - ,计算两整数 a .b 之和. 示例 1: 输入: a = 1, b = 2 输出: 3 示例 2: 输入: ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
随机推荐
- From COM to COM 侯捷 1998.06.12
摘要: 本文簡介 C++ Object Model 和 Component Object Model 的基本概念,並引介四本書籍: 1. Inside The C++ Object Model 2. ...
- 【leetcode 3. 无重复字符的最长子串】解题报告
思路:滑动窗口的思想 方法一:滑动窗口 int lengthOfLongestSubstring(string s) { /* 控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是 ...
- 发布一个npm包
前言 我这里是写了一个vue轮播图插件,因此我使用了vue的脚手架工具创建一个项目,当然你也可以选择自己搭建脚手架. 本例中我会使用vue脚手架创建一个项目,并发布到npm上面去. 通过脚手架创建项目 ...
- 20165224 陆艺杰 Exp 8 Web基础
.基础问题回答 (1)什么是表单 html的一个控件 表单在网页中主要负责数据采集功能 (2)浏览器可以解析运行什么语言 html xml jsp php python 等 (3)WebServer支 ...
- 由奇葩cookie导致服务器500来认识cookie
问题:cookie中文会导致服务器报500错误. 一:cookie的特点 1.以键值对的形式出现的,比如:a=b;b=c 2.中文的值需要转义 cookie的例子 <!DOCTYPE html& ...
- 2、kvm基础常用命令操作
KVM 虚拟机默认的配置文件在 /etc/libvirt/qemu 目录下,默认是以虚拟机名称命名的.xml文件,如下: root@xuedianhu:~# ls /etc/libvirt/qemu ...
- hutool java工具架包功能介绍
https://blog.csdn.net/lx1309244704/article/details/76459718
- RMAN restore fails with ORA-01180: can not create datafile 1 (文档 ID 1265151.1)
http://blog.itpub.net/26655292/viewspace-2131269/ ########Q&A issue1:ORA-01180: can not create d ...
- CodeForces - 95B
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal represe ...
- MapReduce 二次排序
默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...