1. Two Sum&&15. 3Sum&&18. 4Sum
题目:
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
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 unique triplets in the array which gives the sum of zero.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
思路:
这三道题本质上属于同一问题:给定一个数组和目标target,求k个元素,使得k个元素相加的和为target。可能出现的变式为:1.求元素的下标;2.不得重复使用同一元素等,下面进行分析。
对于2Sum而言,要求a+b=target,也就是任意选定元素a,寻找数组中是否有元素b使得b=target-a。可以选择方法有:
方法一:枚举所有的2-subset, 那么这样的复杂度就是从N选出2个,复杂度是O(N^2)
方法二:对数组进行排序并利用头尾两个指针找到两个数使得他们的和等于target。
对于3Sum而言,要求a+b+c=target,这道题可以转化为2Sum问题。即任意选定元素a,在剩余的元素中查找是否存在2个数使得b+c=targe-a。
对于4Sum而言,也可以转化为2Sum问题。任意选定元素a和b,在剩余的元素中查找是否存在2个数使得c+d=targe-a-b。
代码:
2Sum:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int l = ;
int r = nums.size() - ;
int i = ;
vector<int> result;
multimap<int, int> m;
multimap<int, int>::iterator itmulti;
for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++) {
int temp = *it;
m.insert(make_pair(temp, i++));
}
sort(nums.begin(), nums.end());
while (l < r) {
if (nums[l] + nums[r] == target) {
if (nums[l] == nums[r]) {
for (itmulti = m.equal_range(nums[l]).first;
itmulti != m.equal_range(nums[l]).second;
itmulti++) {
result.push_back((*itmulti).second);
}
} else {
itmulti = m.equal_range(nums[l]).first;
result.push_back((*itmulti).second);
itmulti = m.equal_range(nums[r]).first;
result.push_back((*itmulti).second);
}
break;
} else if (nums[l] + nums[r] < target) {
l++;
} else {
r--;
}
}
return result;
}
};
3Sum:
class Solution {
public:
vector<vector<int> > threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int> > results;
for (int i = ; i < (signed) nums.size() - ; i++) {
if (i > && nums[i] == nums[i - ])
continue;
int l = i + ;
int r = nums.size() - ;
while (l < r) {
if (nums[l] + nums[r] + nums[i] < ) {
l++;
} else if (nums[l] + nums[r] + nums[i] > ) {
r--;
} else {
vector<int> result;
result.push_back(nums[i]);
result.push_back(nums[l]);
result.push_back(nums[r]);
results.push_back(result);
while (l < r && nums[l] == nums[l + ]) {
l++;
}
while (l < r && nums[r] == nums[r - ]) {
r--;
}
l++;
r--;
}
}
}
return results;
}
};
4Sum:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int l = ;
int r = nums.size() - ;
int i = ;
vector<int> result;
multimap<int, int> m;
multimap<int, int>::iterator itmulti;
for (vector<int>::iterator it = nums.begin(); it != nums.end(); it++) {
int temp = *it;
m.insert(make_pair(temp, i++));
}
sort(nums.begin(), nums.end());
while (l < r) {
if (nums[l] + nums[r] == target) {
if (nums[l] == nums[r]) {
for (itmulti = m.equal_range(nums[l]).first;
itmulti != m.equal_range(nums[l]).second;
itmulti++) {
result.push_back((*itmulti).second);
}
} else {
itmulti = m.equal_range(nums[l]).first;
result.push_back((*itmulti).second);
itmulti = m.equal_range(nums[r]).first;
result.push_back((*itmulti).second);
}
break;
} else if (nums[l] + nums[r] < target) {
l++;
} else {
r--;
}
}
return result;
}
};
1. Two Sum&&15. 3Sum&&18. 4Sum的更多相关文章
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- [LeetCode] 18. 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- [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】18. 4Sum 四数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
随机推荐
- 【Python全栈-后端开发】Django进阶2-Form表单
Django进阶2-Form表单 Django的Form主要具有一下几大功能: 生成HTML标签(可以保留上次输入内容) 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页 ...
- shell编程之awk命令详解
shell编程之awk命令详解 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; out ...
- Docker入门3------手动编辑自定义镜像
手动编辑自定义镜像 查看本地现有镜像: 基于centos创建一个,会自动下载centos最新原始镜像 docker run -it --name=web centos /bin/bash 然后在容器内 ...
- 1、vue 笔记之 组件
1.组件个人理解: <组件>是页面的一部分,将界面切分成部分,每部分称为 <组件> 2.组件化思想: //2.1.定义一个全局的组件,组件支持‘驼峰命名 ...
- Spring 学习教程(一):浅谈对Spring IOC以及DI的理解
一.个人对IoC(控制反转)和DI(依赖注入)的理解我们平时在开发java web程序的时候,每个对象在需要使用它的合作对象时,自己都要将它要合作对象创建出来(比如 new 对象),这个合作对象是由自 ...
- opencart3如何安装模板
opencart 3模板采用twig模式,安装模板也有点不大一样,随ytkah一起来看看opencart3如何安装模板吧1.下载模板文件,用ftp上传到对应的位置,一般有几个文件夹,比如:admin. ...
- JavaScript基础理解及技巧(入门)
1.java和JavaScript的区别: (1)js只需要解释就可以执行了,而java需要先编译成字节码文.JavaScript的运行只需要浏览器的支持,而java的运行需要JVM(java虚拟机) ...
- IdentityServer4授权和认证集成Identity和profile
identiyt的使用可以看之前的文章:https://www.cnblogs.com/nsky/p/10323415.html 之前的ids4授权服务器都是用的in-men方式把数据添加到内存, 现 ...
- JS数据类型及函数的预编译
1.JS总体上分为:原始值和引用值 原始值分为:Number.Boolean.String.undefined.null;原始值不可改变的值,存储在栈[stack]的,先进后出! 引用值:array. ...
- 2017-2018-1 20155228 《数学建模》 MatlabR2017a安装教程
2017-2018-1 20155228MatlabR2017a安装教程 原版软件和破解补丁的下载 原版软件和破解补丁的下载链接 需要关注微信公众号才能获取下载密码,照办就是了,为了学习嘛哈哈哈 有三 ...