摘要:找符合條件的總數(shù)。雙指針區(qū)間考慮邊界,長度,為空,等。之后的范圍用雙指針和表示。若三個指針的數(shù)字之和為,加入結(jié)果數(shù)組。不要求,所以不用判斷了。同理,頭部兩個指針向后推移,后面建立左右指針夾逼,找到四指針和為目標(biāo)值的元素。
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總數(shù)。
Key:
雙指針
區(qū)間
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,為空,等。
對給定數(shù)組排序。
定一個指針i,從頭開始循環(huán)推進(jìn)。若重復(fù),則跳過。
i之后的范圍用雙指針left和right表示。
若三個指針的數(shù)字之和為0,加入結(jié)果數(shù)組。雙指針繼續(xù)移動,若重復(fù),則跳過。
若三個指針的數(shù)字之和小于0,左指針后移。
若三個指針的數(shù)字之和大于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從數(shù)組頭部向后推移,在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
同理,頭部兩個指針向后推移,后面建立左右指針夾逼,找到四指針和為目標(biāo)值的元素。
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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/66045.html
摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖恪K栽谶@里做個索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。 順序整理 1~50 1...
摘要:三元組相加獲得結(jié)果最接近給定一個數(shù)組,選擇三個元素相加,結(jié)果必須為所有三元組中最接近的值,返回這個三元組的和。思路思路參照三元組相加獲得只需要將上述文章思路中換成第二次循環(huán)找到三元組的和最接近的組合即可。代碼本題以及其它題目代碼地址地址 三元組相加獲得結(jié)果最接近target 3SumClosest 給定一個數(shù)組,選擇三個元素相加,結(jié)果必須為所有三元組中最接近target的值,返回這個...
摘要:為了避免得到重復(fù)結(jié)果,我們不僅要跳過重復(fù)元素,而且要保證找的范圍要是在我們最先選定的那個數(shù)之后的。而計算則同樣是先選一個數(shù),然后再剩下的數(shù)中計算。 2Sum 在分析多數(shù)和之前,請先看Two Sum的詳解 3Sum 請參閱:https://yanjia.me/zh/2019/01/... 雙指針法 復(fù)雜度 時間 O(N^2) 空間 O(1) 思路 3Sum其實可以轉(zhuǎn)化成一個2Sum的題,...
摘要:不同數(shù)包含重復(fù)數(shù)為的時候,表示在外層的循環(huán)正在被使用,所以當(dāng)前循環(huán)遇到為一定要跳過。對當(dāng)前循環(huán)要添加的數(shù)組,在添加當(dāng)前元素后進(jìn)行遞歸,遞歸之后要將當(dāng)前元素的使用標(biāo)記改為,表示已經(jīng)使用和遞歸完畢,然后再將這個元素從的末位刪除。 Subsets Problem Given a set of distinct integers, nums, return all possible subse...
閱讀 2305·2021-09-30 09:47
閱讀 2223·2021-09-26 09:55
閱讀 2954·2021-09-24 10:27
閱讀 1543·2019-08-27 10:54
閱讀 971·2019-08-26 13:40
閱讀 2499·2019-08-26 13:24
閱讀 2423·2019-08-26 13:22
閱讀 1735·2019-08-23 18:38