题目:

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 abc 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 abc, 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的更多相关文章

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

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

  3. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

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

  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】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  7. [LeetCode][Python]18: 4Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...

  8. leetcode 15. 3Sum 二维vector

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

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

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

随机推荐

  1. Mycat原理、应用场景

    Mycat原理 Mycat的原理并不复杂,复杂的是代码,如果代码也不复杂,那么早就成为一个传说了.Mycat的原理中最重要的一个动词是“拦截”,它拦截了用户发送过来的SQL语句,首先对SQL语句做了一 ...

  2. css3 伸缩布局 display:flex等

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. SpringBoot-性能优化之扫包优化

    性能优化 组件自动扫描带来的问题 默认情况下,我们会使用 @SpringBootApplication 注解来自动获取应用的配置信息,但这样也会给应用带来一些副作用.使用这个注解后,会触发自动配置( ...

  4. SVProgressHUD提示框IOS

    SVProgressHUD--比MBProgressHUD更好用的 iOS进度提示组件 项目里用到SVProgressHud,感觉背景颜色太丑,因为很久很久以前改过,就想在这个项目里也改下,但是时间过 ...

  5. 极验验证使用-滑动&选字验证码

    准备 SDK下载 首先在极验官网下载好SDK,附上官网链接,点此可直接下载python版zip包. 模块安装 使用该SDK时发现它依赖两个模块,分别是geetest和requests. pip ins ...

  6. Python Pyinstaller打包含pandas库的py文件遇到的坑

    今天的主角依然是pyinstaller打包工具,为了让pyinstaller打包后exe文件不至过大,我们的py脚本文件引用库时尽可能只引用需要的部分,不要引用整个库,多使用“from *** imp ...

  7. windows程序设计 加载位图图片

    现在网上随便下个jpg图片,用windows自带的画图工具打开,点击画图工具左上角,文件->另存为->选择bmp,点击保存,保存好后,就得到一张位图了. 得到的位图,位图的内存比原图片jp ...

  8. visual studio code常用插件

    1.auto close tag2.chinese language pack for visual studio code3.debugger for chrome4.docker5.html cs ...

  9. ASP.NET MVC案例教程(五)

    ASP.NET MVC案例教程(四) 前言 通过前几篇文章,我们已经能比较自如的使用ASP.NET MVC来呈现页面和数据了.但是,有一个大问题没有解决:如何处理表单数据.例如,我们将要实现的公告发布 ...

  10. delphi “div”、“mod”、“\”除法运算符的区别与使用方法(附带FORMAT使用方法)

    Delphi中和除法相关的算术运算符有: div.mod和符号“\” 下面分别对他们的作用.操作数类型和返回值类型进行一下介绍: div:对2个整数进行除,取商,操作数需是integer类型,返回值也 ...