leetcode 15. 3Sum 二维vector
2016-03-24 16:45
666 查看
传送门
Question
Total Accepted: 108534 Total Submissions: 584814 Difficulty: Medium
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:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
Subscribe to see which companies asked this question
Hide Tags
Array Two Pointers
Show Similar Problems
15. 3Sum
My SubmissionsQuestion
Total Accepted: 108534 Total Submissions: 584814 Difficulty: Medium
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:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)
Subscribe to see which companies asked this question
Hide Tags
Array Two Pointers
Show Similar Problems
Submission Details
311 / 311 test cases passed. | Status:Accepted |
Runtime: 76 ms | Submitted: 0 minutes ago |
class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; vector<int> tmp; int n = nums.size(); sort(nums.begin(),nums.end()); int i,j,k; for(i = 0;i < n;i++){ if(i != 0 && nums[i] == nums[i-1]) continue; for(j = i + 1;j < n;j++){ if( (j != i + 1) && (nums[j] == nums[j-1]) ) continue; for(k = j + 1;k < n;k++){ if( (k != j+1 ) && (nums[k] == nums[k-1]) ) continue; if(nums[i] + nums[j] + nums[k] == 0){ tmp.clear(); tmp.push_back(nums[i]);tmp.push_back(nums[j]);tmp.push_back(nums[k]); ans.push_back(tmp); } } } } return ans; } };
相关文章推荐
- BVH文件格式解析
- 【bzoj2631】【tree】【lct】
- vijos 1011 清帝之惑之顺治 (记忆化搜索)
- storm, kafka集成之本地开发、测试
- 万花筒 | 背完这444句,你的口语绝对不成问题了
- Ajax实战项目1 验证用户的唯一性 环境(Ajax+struts2)
- Java中的序列化Serialable高级详解
- iOS随机数生成
- Android开发_如何调用系统默认浏览器访问
- 通往架构师之路
- hadoop复合键排序使用方法
- https://github.com/akullpp/awesome-java
- 一个映射到mac风格按键的AHK脚本(替换虚拟机键盘映射)
- Apache配置基于域名的虚拟主机
- Struts2当调用action时出现HTTP Status 404 问题解决
- 7.自定义Dialog提示框
- C++文件操作详解http://www.cnblogs.com/likebeta/archive/2012/06/16/2551662.html
- pulltorefresh(二)的自定义属性介绍、attrs文件介绍
- [Android]ListView中分割线的设置
- iOS App集成Apple Pay 编程指南