leetcode 15. 3Sum 双指针
给n个数, 找出三个数相加结果为0的所有的组, 不可重复。
用双指针的思想,O(n^2)暴力的找, 注意判重复。
- class Solution {
- public:
- vector<vector<int>> threeSum(vector<int>& nums) {
- int sz = nums.size();
- vector <vector<int> > ans;
- vector <int> tmp;
- sort(nums.begin(), nums.end());
- for(int i = ; i<sz; i++) {
- if(i> && nums[i] == nums[i-])
- continue;
- int l = i+, r = sz-;
- while(l<r) {
- int sum = nums[i]+nums[l]+nums[r];
- if(sum < ) {
- l++;
- } else if (sum>) {
- r--;
- } else {
- tmp.push_back(nums[i]);
- tmp.push_back(nums[l++]);
- tmp.push_back(nums[r--]);
- sort(tmp.begin(), tmp.end());
- ans.push_back(tmp);
- tmp.clear();
- while(l<r&&nums[l-]==nums[l])
- l++;
- while(r>l&&nums[r] == nums[r+])
- r--;
- }
- }
- }
- return ans;
- }
- };
leetcode 15. 3Sum 双指针的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [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——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=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 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
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 & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
随机推荐
- 个人信用卡管理 - iOS UI原型
- (92) Web Crawling: How can I build a web crawler from scratch? - Quora
(92) Web Crawling: How can I build a web crawler from scratch? - Quora How can I build a web crawler ...
- Jquery读取URL参数
$(document).ready(function(){ function getQueryString(name) { var reg = new RegExp("(^|&)&q ...
- 史上最简单的Hibernate入门简单介绍
事实上Hibernate本身是个独立的框架,它不须要不论什么web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了非常多非Hibernate ...
- c 查找A字符串在B字符串中是否存在,计算出现的次数
主要是应用了头文件<string.h>中的strstr函数 char * strstr(const char *s1, const char *s2); 查找是否存在: #include& ...
- .NET WIN7+IIS 7.5下URLRewriter组件伪静态设置
原文地址:WIN7+IIS 7.5伪静态的设置 --------------------------------------------------------偶是分割线君-------------- ...
- //相当于深拷贝一份dataArray。这样才不会改变dataArray本身的值
//相当于深拷贝一份dataArray.这样才不会改变dataArray本身的值 NSMutableArray* commitDataArray = [NSKeyedUnarchiver unarch ...
- Java如何实现对Mysql数据库的行锁
场景如下: 用户账户有余额,当发生交易时,需要实时更新余额.这里如果发生并发问题,那么会造成用户余额和实际交易的不一致,这对公司和客户来说都是很危险的. 那么如何避免: 网上查了下,有 ...
- org.apache.tomcat.util.bcel.classfile.ClassFormatException: null is not a Java .class file
org.apache.tomcat.util.bcel.classfile.ClassFormatException: null is not a Java .class file 在$TOMCA ...
- 在Oracle 11g中用看Oracle的共享内存段---------IPCS
很早之前,在一次讲课了,用了命令ipcs,发现oracle的共享内段好小,如下: oracle@mydb ~]$ ipcs -a ------ Shared Memory Segments ----- ...