4sum

[抄题]:

[思维问题]:

  1. 以为很复杂,其实是“排序+双指针”的最高阶模板

[一句话思路]:

[输入量特别大怎么办]:

[画图]:

[一刷]:

  1. 先排序
  2. if (i > 0 && nums[i] == nums[i - 1]) ,  之前的已经操作过,
    continue;退出

  3. ArrayList<Integer> temp = new ArrayList<Integer>();//临时数组在哪里用就在哪里临时定义
  4. 等于target时要继续left++ right--
  5. corner case 一定是返回rst自身[],不是null字母

[总结]:

先排序,然后一吃通吃

[复杂度]:

[英文数据结构,为什么不用别的数据结构]:

  1. res数组用linkedlist arraylist都可以

[其他解法]:

[Follow Up]:

[题目变变变]:

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> rst = new LinkedList<List<Integer>>(); if (nums == null || nums.length < 4) {
return rst;
} Arrays.sort(nums); for(int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1])
continue; for(int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1])
continue; int left = j + 1;
int right = nums.length - 1;
while(left < right) {
if (nums[i] + nums[j] + nums[left] + nums[right] <target) {
left++;
}
else if (nums[i] + nums[j] + nums[left] + nums[right] >target) {
right--;
}
else {
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
while (left < right && nums[right] == nums[right - 1]) {
right--;
} List<Integer> temp = new LinkedList<Integer>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[left]);
temp.add(nums[right]);
rst.add(temp);
left++;
right--;
}
}
}
}
return rst;
}
}

4sum 数组

[抄题]:

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2] Output:
2 Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

[思维问题]:

  1. 不能利用key的唯一性:把两个元素加在一起作为一个整体即可
  2. 用map.getOrDefault(sum, 0)可以查找或初始定义出value的值

[一句话思路]:

用hashmap,把两个数的和作为一组,先存后查

[输入量特别大怎么办]:

[画图]:

[一刷]:

  1. 用get函数查的,老师说空指针错误

[总结]:

没有某个sum做index的时候很尴尬。此题两次运用了机智的map.getOrDefault(sum,0)函数

[复杂度]:

Time complexity: O(n^2) Space complexity: O(n^2)

新建一个HashMap不需要给定N空间,是根据新的Key来分配空间,Key的数量是可预测的。

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int rst = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i = 0; i < A.length; i++) {
for(int j = 0; j < B.length; j++) {
int sum = A[i] + B[j];
map.put(sum,map.getOrDefault(sum,0) + 1);
}
} for(int i = 0; i < C.length; i++) {
for(int j = 0; j < D.length; j++) {
rst += map.getOrDefault(-(C[i] + D[j]),0);
}
}
return rst;
}
}

4sum, 4sum closest的更多相关文章

  1. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  2. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  3. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  4. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

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

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

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

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

  8. 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line

    Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...

  9. 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), ...

随机推荐

  1. mongo获取lbs数据

    进入mongo目录执行./mongo 命令 #切换数据库use coachloc db.runCommand({geoNear : "coachloc" ,near : [113. ...

  2. [UE4]为什么会有类型检查

    类型检查 定义:对象的行为是否符合类型的行为 作用:帮助开发者找出潜在的错误. 类型转换 隐式类型转换:整数可以和浮点数运算 显式类型转换/强制类型转换

  3. (转)USB中CDC-ECM的了解和配置

    USB中典型类及子类: 类别 解释 子类 典型应用 IC芯片 备注 UVC 视频类 免驱USB摄像头 CDC 通讯类 RNDIS ECM(p24) 免驱USB网卡 RTL8152B EEM ..... ...

  4. 取得 Ajax 返回参数

    ajax 函数,p1 为正常参数 function ExecWebFunction(callback,p1) { $.ajax({ type: "POST", contentTyp ...

  5. 学习MongoDB 五: MongoDB查询(数组、内嵌文档)(二)

    一.简介 我们上一篇介绍了db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段,我们今天介绍了对数组和内嵌文档的查询操作,尤其是 ...

  6. ajax 方法的使用以及方法中各参数的含义

    由于近来经常在项目中使用 ajax 这个函数,在工作之余自己查找了相关的资料,并总结了 ajax 方法的使用,以及方法中各个参数的含义,供大家学习参考使用 type: 要求为String类型的参数,请 ...

  7. Flutter,最好的跨平台开发框架

    今天说说使用flutter的一些体会 对于Flutter,从发现到接触再到使用,不知不觉,已经有大半年了!在这段时间里,谷歌几乎每天都会更新Flutter,有时甚至一天更新几次,这让我对它更加充满信心 ...

  8. linux获取日志指定行数范围内的内容

    假如我要获取“浅浅岁月拂满爱人袖”到“落入凡尘伤情着我”之间的内容. 1.首先得到他们所在的行号: -n选项显示行号 但是有时候grep后显示的是“匹配到二进制文件(标准输入)”,说明搜索的字符串在某 ...

  9. leetcode171

    public class Solution { private int ConvertToC(char c) { ; switch (c) { case 'A': case 'a': rnt = ; ...

  10. svn异常处理

    TortoiseSVN 为客户端,SUBVersion为服务器端. 1.安装的tortoiseSVN不在鼠标右键菜单栏 出现这种原因是电脑的系统和svn不符,即电脑是64位系统,而svn是32位的. ...