//用类似双指针的方法,确定第一个i的位置后,j和k向左向右移动使nums[j]+nums[k] = -nums[i];注意特判

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.empty()||nums.size() < ) return {};
set<vector<int>> res;
sort(nums.begin(),nums.end());
for(int i=;i < nums.size()-;i++){
for(int j=i+,k=nums.size()-;j < k;){
int sum = nums[j] + nums[k];
if(sum < (-nums[i])){j++;}
else if(sum > (-nums[i])){k--;}
else{
vector<int> cnt;
cnt.push_back(nums[i]);
cnt.push_back(nums[j]);
cnt.push_back(nums[k]);
res.insert(cnt);
j++;k--;
}
}
}
return vector<vector<int>>(res.begin(),res.end());
}
};

Leetcode 15的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. [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 ...

  3. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  4. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

  5. 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 ...

  6. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  7. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  8. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

  9. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  10. Leetcode 15. Sum(二分或者暴力或者哈希都可以)

    15. 3Sum Medium Given an array nums of n integers, are there elements a, b, c in nums such that a +  ...

随机推荐

  1. poj1191 棋盘分割【区间DP】【记忆化搜索】

    棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16263   Accepted: 5812 Description ...

  2. c# 自定义控件之小小进度条

    先看效果图: 非常简洁的一个进度条. 完整项目源码下载:http://files.cnblogs.com/files/tuzhiyuan/%E8%BF%9B%E5%BA%A6%E6%9D%A1%E6% ...

  3. JQuery的Ajax跨域请求的解决方式

            今天在项目中须要做远程数据载入并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究 ...

  4. 002-spring cache 基于声明式注解的缓存-02-CachePut、CacheEvict、Caching、CacheConfig、EnableCaching、自定义

    1.2.CachePut annotation 在支持Spring Cache的环境下,对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素 ...

  5. python+Nginx+uWSGI使用说明

    安装环境 Remote: CentOS 7.4 x64 (django.example.com) Python: Python3.6.5 Django: Django 2.0.4 nWSGI:  uw ...

  6. 4.9 Routing -- Query Parameters

    一.概述 1. 在URL中查询参数是可选的key-value对,出现在?的右边.例如,下面的URL有两个查询参数,sort和page,对应的值分别是ASC和2. example:http://exam ...

  7. 198. House Robber(动态规划)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON MoveRectangle

    zw版[转发·台湾nvp系列Delphi例程]HALCON MoveRectangle procedure TForm1.Button1Click(Sender: TObject);var img : ...

  9. Adobe AIR中使用Flex连接Sqlite数据库(1)(创建数据库和表,以及同步和异步执行模式)

    系列文章导航 Adobe AIR中使用Flex连接Sqlite数据库(1)(创建数据库和表) Adobe AIR中使用Flex连接Sqlite数据库(2)(添加,删除,修改以及语句参数) Adobe ...

  10. Vue学习笔记之Babel介绍

    这个是解析我们es6的代码的,为什么要用它呢,因为对于一些ie浏览器,甚至FF浏览器,低版本的还不能识别我们的es6代码,那么vue里面好多还让我们去写es6的代码,这个时候我们就可以用babel这个 ...