Leetcode Week11
来源:互联网 发布:模块化数据机房 编辑:程序博客网 时间:2024/05/22 14:37
/*
在数组中找所有3个数相加等于0的子数组
Given an array S of n integers, are there elements a, b, c in S 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.
*/
class Solution {public: vector<vector<int> >* v; vector<vector<int>> threeSum(vector<int>& num) { v = new vector<vector<int> >(); if(num.size()<3) { return *v; } sort(num.begin(),num.end()); for(int i=0;i<num.size()-2;i++) { if(i>0 && num[i]==num[i-1]) continue; for(int j=i+1;j<num.size();j++) { if(j>i+1 && num[j]==num[j-1]) continue; int left = j+1; int right = num.size()-1; while(left<=right) { int mid = left+(right-left)/2; if(num[i]+num[j]+num[mid] == 0) { vector<int> tmp; tmp.push_back(num[i]); tmp.push_back(num[j]); tmp.push_back(num[mid]); v->push_back(tmp); break; } else if(num[i]+num[j]+num[mid]>0) { right = mid-1; } else { left = mid+1; } } } } return *v; } };
0 0
- Leetcode Week11
- leetcode week11
- Leetcode Week11
- LeetCode Week11: Burst Ballons
- LeetCode题解 week11
- Leetcode: 43.Multiply Strings(Week11, Medium)
- week11-leetcode #451-Sort-Characters-By-Frequency
- 算法设计Week11 LeetCode Algorithms Problem #303 Range Sum Query
- [LeetCode-Algorithms-48] "Rotate Image" (2017.11.16-WEEK11)
- LeetCode#238 Product of Array Except Self (week11)
- week6-week11
- Algorithm-week11
- 算法Week11.01 - LeetCode 452. Minimum Number of Arrows to Burst Balloons
- 算法设计Week11 LeetCode Algorithms Problem #304 Range Sum Query 2D
- [LeetCode-Algorithms-19] "Remove Nth Node From End of List" (2017.11.16-WEEK11)
- C程序设计Week11作业
- No11.week11 maxSubArray
- sql server code week11
- redis集群的搭建(伪集群)
- 在android中service启动异常
- MySQL日记——锁机制
- 最新Eric Python IDE 安装方法Eric6_17.04.1 + PyQt5_5.8.2
- jquery-zclip插件
- Leetcode Week11
- [李景山php] 20170505深入理解PHP内核[读书笔记]--第二章:用户代码执行--生命周期和Zend引擎
- 5.分布式增删改查
- hdoj 5883 The Best Path
- Java泛型_自定义泛型接口
- linux shell基础二之命令行参数
- Python Notes (3)
- 页面置换算法
- JS iterable 学习笔记