LeetCode OJ:Three Sum(三数之和)
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)
大体的思想先将数组排序,从小到大取vector中的数first,再从剩下的数中取和等于 0 - first 的数即可。下面是代码(一开始没想出来,然后参考了别人的解法在写出来,一般的三层循环谁都能想到,但是时间复杂度太高,这里的这个时间复杂度应该是O(N^2),还是可以接受的)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> result;
int sz = nums.size();
sort(nums.begin(), nums.end());
for (int i = ; i < sz - ; ++i){
twoSum(nums, i + , - nums[i], result);
while(nums[i] == nums[i + ]) ++i;//这一步要注意,防止得出重复的vector
}
return result;
}
void twoSum(vector<int> & nums, int start, int value, vector<vector<int>> & ret)
{
int beg = start;
int end = nums.size()-;
while (beg < end){
int sum = nums[beg] + nums[end];
if (sum < value)
beg++;
else if (sum > value)
end--;
else{
ret.push_back(vector<int>{nums[start - ], nums[beg], nums[end]});
while (nums[beg + ] == nums[beg]) beg++;//这一步的处理应该注意,防止出现相同的vector
while (nums[end - ] == nums[end]) end--;
beg++, end--;
}
}
}
};
java版的代码如下所示:
(由于不太熟悉ArrayList和List之间的关系,写起来感觉各种坑爹啊,注意下List和ArrayList之间的各种转换就可以了,代码如下):
public class Solution {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; ++i){
twoSum(nums, i+1, 0 - nums[i]);
while(i < nums.length - 2 && nums[i] == nums[i+1])
++i;
}
return ret;
}
public void twoSum(int[] nums, int start, int value)
{
int beg = start;
int end = nums.length - 1;
while(beg < end){
if(nums[beg] + nums[end] == value){
List<Integer> list = new ArrayList<Integer>();
list.add(nums[start - 1]);
list.add(nums[beg]);
list.add(nums[end]);
ret.add(list);
while(beg < end && nums[beg+1] == nums[beg])
beg++;
while(beg < end && nums[end-1] == nums[end])
end--;
beg++;
end--;
}else if(nums[beg] + nums[end] > value){
end--;
}else{
beg++;
}
}
}
}
PS:同样的4Sum问题也可以转换成上面的3Sum问题,从而递归的求解,KSum问题也是一样
LeetCode OJ:Three Sum(三数之和)的更多相关文章
- LeetCode#15 | Three Sum 三数之和
一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode 第15题-三数之和
1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 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] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- [leetcode]1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
随机推荐
- 【ORACLE】10步全然卸载CRS
版权声明:本文为博主原创文章(原文:blog.csdn.net/clark_xu 徐长亮的专栏),未经博主同意不得转载. https://blog.csdn.net/u011538954/articl ...
- Win10 IIS 安装.net 4.5
更新Win10,原来的IIS站点访问不了,原因是因为IIS 没有.net 4.5,使用网上的aspnet_regiis.exe -i命令,一点都不靠谱,直接提示: C:\WINDOWS\system3 ...
- CNN学习笔记:卷积神经网络
CNN学习笔记:卷积神经网络 卷积神经网络 基本结构 卷积神经网络是一种层次模型,其输入是原始数据,如RGB图像.音频等.卷积神经网络通过卷积(convolution)操作.汇合(pooling)操作 ...
- 在vps主机上***
一.安装 Shadowsocks服务端: 1.下载软件包 yum install python-setuptools && easy_install pip pip install s ...
- msys2安装开发工具
pacman -Syupacman -Supacman -S base-develpacman -S mingw-w64-x86_64-toolchain
- CWinApp类CMultiDocTemplate类CDocument类CView类的关系
转自:http://blog.csdn.net/bboot/article/details/26884011 不得不转,瞬间搞清了很多问题,短小精悍 1.CWinApp类 它包含并管理着应用程序的 ...
- $python数据分析基础——初识matplotlib库
基本用法 import numpy as np import matplotlib.pyplot as plt # 年份 year = [1950,1970,1990,2010] # 全球总人口(单位 ...
- Ubuntu启动文件破坏启动恢复方法
reboot后主机登录显示如下图: 解决步骤: 1.fs0:(回车) 2.edit startup.nsh 3.添加下面字段: fs0: cd EFI/ubuntu grubx64.efi 4.重启即 ...
- MySQL Binlog解析(1)
一.Binlog File Binlog files start with a Binlog File Header followed by a series of Binlog Event Binl ...
- Nginx 301与302配置
说明 1.首先看一个完整代码示例,关于nginx 301 302跳转的. 301跳转设置: server { listen 80; server_name 123.com; rewrite ^/(.* ...