【Leetcode 15】3Sum 三数和问题 C++
来源:互联网 发布:怎么做数据分析表 编辑:程序博客网 时间:2024/06/06 06:59
3Sum三数和问题页
问题简述:给定一个数组,找出其中所有不同的三数和等于0的组合。
问题分析:
首先,两数和问题这样做。先对数组中的数进行排序,再设置两个指针,一个指向头,一个指向尾。判断两数和是否等于想要的数,如果是则在结果集添加这个数组;如果小了说明左边指针指向的数小了,因此左指针右移;反之如果大了则右指针左移。
尝试把三数和问题转化为两数和问题:同样先对数组排序,设置三个指针p,q,r,p指针指向第一个数x,则q,r要指向数组中剩余数中的两个,并且指向的两数和为-x,从而转化为两数和问题。对p指向第一个数的情况分析完毕后,不可能再有满足题意且包含x的情况,于是p右移。这样一直分析到p指向数组中倒数第三个数的情况。注意跳过所有重复的情况。
经验:
vector的使用:
①vector<vector<int> >
注意两个“>”之间要有空格,否则会因为运算符“>>”有歧义;
②vector调用构造函数时,可以用vector num(3),用参数3初始化向量中有三个数;
③vector另外一个构造函数,vector num(a, a+len),a为数组,len为a中元素个数,则将数组a中的元素构建为向量。sort使用:
①sort在algorithm头文件中
②sort用法,sort(num.begin(), num.end())
代码:
(此处参考哈哈的博客)
#include <iostream>#include <vector> #include <algorithm>using namespace std;class Solution{public: vector< vector<int> > threeSum(vector<int> &num) { vector<int> numSet(3); vector< vector<int> > r; // 1.排序 sort(num.begin(), num.end()); int sum; int len = num.size(); // 2.拿出第一个数,转化为两数和问题。注意外层循环到倒数第三个数即可 for(int i = 0; i < len-2; i++) { sum = 0 - num[i]; numSet[0] = num[i]; // 3.两数和问题 for(int j = i+1, k = len-1; j < k;) { if(num[j] + num[k] == sum) { numSet[1] = num[j++]; numSet[2] = num[k--]; r.push_back(numSet); // 根据题目要求,跳过重复元素 while(j < k && num[j] == num[j-1]) j++; while(j < k && num[k] == num[k+1]) k--; } else if(num[j] + num[k] < sum) j++; else k--; } while(i < len-2 && num[i+1] == num[i]) i++; } return r; }};int main() { int a[] = {-2, 13, -5, -4, -7, 8, 0, -9, 6, 7, 0, -4, 2, 1, -2, 4}; int len = sizeof(a) / sizeof(a[0]); vector<int> num(a, a+len); Solution s; vector< vector<int> > result; result = s.threeSum(num); // 输出结果 for(int q = 0; q < result.size(); q++) { vector<int> x = result[q]; cout << "("; int t; for(t = 0; t < x.size()-1; t++) { cout << x[t] << " "; } cout << x[t]; cout << ")" << endl; } return 0;}
0 0
- 【Leetcode 15】3Sum 三数和问题 C++
- LeetCode OJ-15.3Sum(三数和问题)
- LeetCode | 15. 3Sum(三数和为定值)
- Leetcode #15 3Sum 三数之和 解题小节
- Leetcode: 15. 3Sum 三数之和
- leetcode-3sum 三数之和
- [LeetCode] 3Sum 三数之和 Python
- LeetCode OJ-16.3Sum Closest(最接近三数和)
- 3Sum(三数和)
- LeetCode 15. 3Sum(三数之和)
- [LeetCode] 3Sum Closest 最近的三数之和 Python
- leetcode 15 3sum问题
- [Leetcode P15] Three Sum 三数之和
- [c++] LeetCode Two Sum问题
- LeetCode | 3Sum(三个数的和等于0)
- LeetCode OJ 之 3Sum (三个数的和)
- leetcode:3Sum问题
- leetcode 3sum问题
- 复合索引和多个单列索引的效率比较
- 快速排序C++实现
- 框架(三)事务
- sqlmap常用语句
- mybatis
- 【Leetcode 15】3Sum 三数和问题 C++
- 线程属性
- 感知机模型学习笔记及Python实现
- javascript Date类型 转 年月日 格式的 字符串
- 2016.08.14【初中部 NOIP提高组 】模拟赛C小结
- scala基础------>定义容器
- 排序
- Redis的String类型操作
- Android之ListView展示多类型的条目