文章目录

Two Sum

  • 题意

    给定一个数组,和指定一个目标和。从数组中选择两个数满足和为目标和。保证有且只有一个解。每个元素只可以用一次。
  • 思路

    Hash表快速查询值是否在数组中存在。

    枚举一个数,查询另一个数是否存在。

    注意:虽然一个元素只可以使用一次,但是数组中可以出现重复的元素。
  • 复杂度

    T(N)=O(N),M(N)=O(N)T(N)=O(N),M(N)=O(N)T(N)=O(N),M(N)=O(N)
  • 结果

    Runtime: 5 ms, faster than 70.02% of Java online submissions for Two Sum.

    Memory Usage: 37.5 MB, less than 100.00% of Java online submissions for Two Sum.

    即(5ms, 70.02%, 37.5MB, 100.0%)
  • 源码
class Solution {
public int[] twoSum(int[] nums, int target) {
int []ans = new int[2];
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
for (int i = 0;i < nums.length; ++i) {
if (m.containsKey(nums[i])) {
if (nums[i]*2 == target) {
ans[0] = m.get(nums[i]);
ans[1] = i;
return ans;
}
} else {
m.put(nums[i], i);
}
}
for (int i = 0;i < nums.length; ++i)
if (nums[i]*2 != target && m.containsKey(target-nums[i])) {
ans[0] = i;
ans[1] = m.get(target-nums[i]);
break;
}
return ans;
}
}

Two Sum II

  • 题意

    题意不变。只是输入数组有序。另外返回的下标是基于1。
  • 思路

    由于有序,维护一左一右两个指针,所指数的和过小,则左指针右移一格;过大,右指针左移一格;等于则返回结果。
  • 复杂度

    T(N)=O(N),M(N)=O(1)T(N)=O(N),M(N)=O(1)T(N)=O(N),M(N)=O(1)
  • 结果

    (0ms, 100.0%, 36.9 MB, 100.0%)
  • 源码
class Solution {
public int[] twoSum(int[] nums, int target) {
// twoSumWithSortedArr
// 已经确保了答案存在
int l = 0;
int r = nums.length-1;
int t;
while (true) {
t = nums[l]+nums[r];
if (t < target)
++l;
else if (t > target)
--r;
else
return new int[] {l+1,r+1};
}
}
}

3Sum

  • 题意

    给定一个数组,从中选三个元素a,b,c.使得a+b+c=0.给出所有的可能的三元组(unique triplets)的集合。
  • 思路1

    原数组分为负数和非负数两个数组。则答案只有这些可能:

    1. 负+非负+非负
    2. 非负+负+负
    3. 非负+非负+非负(实际上这个只能是3个0)

      3个0的特殊判断。

      情况1枚举负数,转换为非负数数组中的two Sum问题。

      情况2同理。
  • 思路2

    先排序,直接枚举一个数。转化为Two Sum II问题。
  • 复杂度

    思路1T(N)=O(N2),M(N)=O(N)T(N)=O(N^2),M(N)=O(N)T(N)=O(N2),M(N)=O(N)

    思路2T(N)=O(N2),M(N)=O(1)T(N)=O(N^2),M(N)=O(1)T(N)=O(N2),M(N)=O(1)

    且考虑到思路2变成复杂度更低。毫无疑问选思路2.
  • 结果

    (37ms, 99.61%, 48.5MB, 100.00%)
  • 源码
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int target = 0;
List<List<Integer>> ans = new ArrayList<List<Integer>>();
Arrays.sort(nums);
int len = nums.length;
for (int i = 0; i+3 <= len; ++i) {
if (nums[i]*3 > target)
break;
if (i > 0 && nums[i] == nums[i-1])
continue;
int part_target = target-nums[i];
int range[] = {i+1,len-1};
while (range[0] < range[1]) {
twoSum(nums,part_target,range);
if (range[0] >= range[1])
break;
// found a ans
List<Integer> l = new ArrayList<Integer>();
l.add(nums[i]);
l.add(nums[range[0]]);
l.add(nums[range[1]]);
ans.add(l);
// move
do{
++range[0];
}while(range[0] < range[1] && nums[range[0]] == nums[range[0]-1]);
}
}
return ans;
}
public void twoSum(int[] nums, int target, int []range) {
int t;
while (range[0] < range[1]) {
t = nums[range[0]]+nums[range[1]];
if (t < target)
++range[0];
else if (t > target)
--range[1];
else
return;
}
}
}

4Sum

  • 题意

    类似于3Sum.只是变成4个数了。并且目标和不是固定的0,而是通过输入参数指定。
  • 思路

    直接循环枚举,注意控制条件以免重复。

    其实应该写搜索的,但是枚举更加无脑暴力,就直接敲了。
  • 复杂度

    T(N)=O(N3),M(N)=O(1)T(N)=O(N^3),M(N)=O(1)T(N)=O(N3),M(N)=O(1)
  • 结果

    (19ms, 89.95%, 40MB, 100.00%)
  • 源码
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
Arrays.sort(nums);
int len = nums.length;
for (int i = 0; i+4 <= len; ++i) {
if (nums[i]*4 > target)
break;
if (i > 0 && nums[i] == nums[i-1])
continue;
for (int j = i+1; j+3 <= len; ++j) {
if (nums[i]+nums[j]*3 > target)
break;
if (j > i+1 && nums[j] == nums[j-1])
continue; // 前一个数并非是a,[j]和前一个数相同,则没必要用[j]再当b
int part_target = target-nums[i]-nums[j];
int range[] = {j+1,len-1};
while (range[0] < range[1]) {
twoSum(nums,part_target,range);
if (range[0] >= range[1])
break;
// found a ans
List<Integer> l = new ArrayList<Integer>();
l.add(nums[i]);
l.add(nums[j]);
l.add(nums[range[0]]);
l.add(nums[range[1]]);
ans.add(l);
// move
do{
++range[0];
}while(range[0] < range[1] && nums[range[0]] == nums[range[0]-1]);
}
}
}
return ans;
}
public void twoSum(int[] nums, int target, int []range) {
int t;
while (range[0] < range[1]) {
t = nums[range[0]]+nums[range[1]];
if (t < target)
++range[0];
else if (t > target)
--range[1];
else
return;
}
}
}

LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解的更多相关文章

  1. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  2. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  3. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  4. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  5. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  6. LeetCode_167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...

  7. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

  8. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  9. 167. Two Sum II - Input array is sorted@python

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

随机推荐

  1. display:table的用法

    目前,在大多数开发环境中,已经基本不用table元素来做网页布局了,取而代之的是div+css,那么为什么不用table系表格元素呢? 1.用DIV+CSS编写出来的文件k数比用table写出来的要小 ...

  2. JS实现斐波那契数列的几种方法

    斐波那契数列指的是这样一个数列:1.1.2.3.5.8.13.21.34.…… 前两项为1,从第三项起,每一项等于前两项的和,即F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n& ...

  3. pyHamcrest

    概念 Hamcrest是用于编写匹配器对象的框架.他提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活.Hamcrest还有很好的可扩展性,能够创建自定义的匹配器. 支持语言 ...

  4. Java基础之六、Java编程思想(8-10)

    八.多态 多态(也称作动态绑定.后期绑定或运行时绑定) 域(成员变量)是不具有多态性的,只有普通的方法调用是多态的,任何域访问操作都将由编译器解析,因此不是多态的 静态方法也是不具有多态性的 publ ...

  5. export和export default的区别

    export和export default的区别一.export的使用1.直接输出export let words = ‘hello world!!!’export function output() ...

  6. Android Studio 快捷方式记录

  7. Github上优秀的.NET Core项目

    Github上优秀的.NET Core开源项目的集合.内容包括:库.工具.框架.模板引擎.身份认证.数据库.ORM框架.图片处理.文本处理.机器学习.日志.代码分析.教程等. Github地址:htt ...

  8. VS自定义模板-以自定义类模板为样例

    前言 在实际的工作过程中部分公司会要求开发人员在开发过程中需遵守一些开发规范,开发规范中主要包括文件的注释规范,项目.文件.变量的命名规范(例如驼峰规范)等等.例如我们代码规范中就有一项新增文件的文件 ...

  9. 这 100 道 Python 题,拿去刷!!!

    2020年,学 Python 还有价值吗? 根据 2020 年 2 月的 TIOBE 编程语言排行榜显示,Python仍然稳居第三位. 此排行榜排名基于互联网上有经验的程序员. 课程和第三方厂商的数量 ...

  10. opencv —— remap 重映射

    重映射的概念 重映射,就是把一幅图像中某位置的像素放置到另一个图片指定位置的过程. 实现重映射:remap 函数 将图像进行重映射几何变换,基于的公式为:dst (x, y) = src ( mapx ...