摘要:找符合条件的总数。双指针区间考虑边界,长度,为空,等。之后的范围用双指针和表示。若三个指针的数字之和为,加入结果数组。不要求,所以不用判断了。同理,头部两个指针向后推移,后面建立左右指针夹逼,找到四指针和为目标值的元素。
Two Sum Problem
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.
NoticeYou may assume that each input would have exactly one solution
Examplenumbers=[2, 7, 11, 15], target=9 return [1, 2]Challenge
Either of the following solutions are acceptable:
O(n) Space, O(nlogn) Time O(n) Space, O(n) TimeNote
需要返回的是index,不能重新sort,所以,不能用双指针法,尽量用HashMap做。
Solution Brute Forcepublic class Solution { public int[] twoSum(int[] numbers, int target) { int[] res = {-1, -1}; if (numbers.length < 2) return res; for (int i = 0; i < numbers.length; i++) { for (int j = i+1; j < numbers.length; j++) { if (numbers[i]+numbers[j] == target) { res[0] = i+1; res[1] = j+1; return res; } } } return res; } }HashMap
public class Solution { public int[] twoSum(int[] A, int target) { int[] res = {-1, -1}; if (A == null || A.length < 2) return res; MapTwo Sum II Problemmap = new HashMap<>(); for (int i = 0; i < A.length; i++) { if (map.containsKey(target-A[i])) { res[0] = map.get(target-A[i])+1; res[1] = i+1; return res; } else map.put(A[i], i); } return res; } }
Given an array of integers, find how many pairs in the array such that their sum is bigger than a specific target number. Please return the number of pairs.
ExampleGiven numbers = [2, 7, 11, 15], target = 24. Return 1. (11 + 15 is the only pair)
ChallengeDo it in O(1) extra space and O(nlogn) time.
Note找符合条件的pair总数。
Key:
双指针
区间
Solutionpublic class Solution { public int twoSum2(int[] nums, int target) { if (nums == null || nums.length == 0) return 0; Arrays.sort(nums); int count = 0; int left = 0, right = nums.length-1; while (left < right) { if (nums[left]+nums[right] > target) { count += right-left; right--; } else { left++; } } return count; } }3Sum Problem
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.
NoticeElements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
ExampleFor example, given array S = {-1 0 1 2 -1 -4}, A solution set is:
(-1, 0, 1) (-1, -1, 2)Note
考虑边界,长度<3,为空,等。
对给定数组排序。
定一个指针i,从头开始循环推进。若重复,则跳过。
i之后的范围用双指针left和right表示。
若三个指针的数字之和为0,加入结果数组。双指针继续移动,若重复,则跳过。
若三个指针的数字之和小于0,左指针后移。
若三个指针的数字之和大于0,右指针前移。
public class Solution { public ArrayList3Sum Closest Problem> threeSum(int[] A) { ArrayList > res = new ArrayList(); if (A.length < 3) return null; Arrays.sort(A); for (int i = 0; i <= A.length-3; i++) { int left = i+1, right = A.length-1; if (i != 0 && A[i] == A[i-1]) continue; while (left < right) { int sum = A[i]+A[left]+A[right]; if (sum == 0) { ArrayList temp = new ArrayList(); temp.add(A[i]); temp.add(A[left]); temp.add(A[right]); res.add(temp); left++; right--; while (left < right && A[left] == A[left-1]) left++; while (left < right && A[right] == A[right+1]) right--; } else if (sum < 0) left++; else right--; } } return res; } }
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.
NoticeYou may assume that each input would have exactly one solution.
ExampleFor example, given array S = [-1 2 1 -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
ChallengeO(n^2) time, O(1) extra space
Note不要求unique triplets,所以不用判断duplicate了。
定指针i从数组头部向后推移,在i的右边建立左右指针left和right,计算三指针和。
用左右指针夹逼找和的最小值并更新。
public class Solution { public int threeSumClosest(int[] A ,int target) { int min = Integer.MAX_VALUE - target; if (A == null || A.length < 3) return -1; Arrays.sort(A); for (int i = 0; i < A.length-2; i++) { int left = i+1, right = A.length-1; while (left < right) { int sum = A[i]+A[left]+A[right]; if (sum == target) return sum; else if (sum < target) left++; else right--; min = Math.abs(min-target) < Math.abs(sum-target) ? min : sum; } } return min; } }4Sum
Description
Notes
Testcase
Judge
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.
NoticeElements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
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)Note
同理,头部两个指针向后推移,后面建立左右指针夹逼,找到四指针和为目标值的元素。
Solutionpublic class Solution { public ArrayList> fourSum(int[] A, int target) { int n = A.length; ArrayList > res = new ArrayList(); Arrays.sort(A); for (int i = 0; i < n-3; i++) { if (i != 0 && A[i] == A[i-1]) continue; for (int j = i+1; j <= n-3; j++) { if (j != i+1 && A[j] == A[j-1]) continue; int left = j+1, right = n-1; while (left < right) { int sum = A[i]+A[j]+A[left]+A[right]; if (sum == target) { ArrayList temp = new ArrayList(); temp.add(A[i]); temp.add(A[j]); temp.add(A[left]); temp.add(A[right]); res.add(temp); left++; right--; while (left < right && A[left] == A[left-1]) left++; while (left < right && A[right] == A[right+1]) right--; } else if (sum < target) left++; else right--; } } } return res; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66045.html
摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...
摘要:三元组相加获得结果最接近给定一个数组,选择三个元素相加,结果必须为所有三元组中最接近的值,返回这个三元组的和。思路思路参照三元组相加获得只需要将上述文章思路中换成第二次循环找到三元组的和最接近的组合即可。代码本题以及其它题目代码地址地址 三元组相加获得结果最接近target 3SumClosest 给定一个数组,选择三个元素相加,结果必须为所有三元组中最接近target的值,返回这个...
摘要:为了避免得到重复结果,我们不仅要跳过重复元素,而且要保证找的范围要是在我们最先选定的那个数之后的。而计算则同样是先选一个数,然后再剩下的数中计算。 2Sum 在分析多数和之前,请先看Two Sum的详解 3Sum 请参阅:https://yanjia.me/zh/2019/01/... 双指针法 复杂度 时间 O(N^2) 空间 O(1) 思路 3Sum其实可以转化成一个2Sum的题,...
摘要:不同数包含重复数为的时候,表示在外层的循环正在被使用,所以当前循环遇到为一定要跳过。对当前循环要添加的数组,在添加当前元素后进行递归,递归之后要将当前元素的使用标记改为,表示已经使用和递归完毕,然后再将这个元素从的末位删除。 Subsets Problem Given a set of distinct integers, nums, return all possible subse...
阅读 2221·2021-09-30 09:47
阅读 2181·2021-09-26 09:55
阅读 2865·2021-09-24 10:27
阅读 1484·2019-08-27 10:54
阅读 934·2019-08-26 13:40
阅读 2462·2019-08-26 13:24
阅读 2364·2019-08-26 13:22
阅读 1685·2019-08-23 18:38