摘要:為了避免得到重復(fù)結(jié)果,我們不僅要跳過重復(fù)元素,而且要保證找的范圍要是在我們最先選定的那個(gè)數(shù)之后的。而計(jì)算則同樣是先選一個(gè)數(shù),然后再剩下的數(shù)中計(jì)算。
2Sum
在分析多數(shù)和之前,請(qǐng)先看Two Sum的詳解
3Sum請(qǐng)參閱:https://yanjia.me/zh/2019/01/...
雙指針法 復(fù)雜度時(shí)間 O(N^2) 空間 O(1)
思路3Sum其實(shí)可以轉(zhuǎn)化成一個(gè)2Sum的題,我們先從數(shù)組中選一個(gè)數(shù),并將目標(biāo)數(shù)減去這個(gè)數(shù),得到一個(gè)新目標(biāo)數(shù)。然后再在剩下的數(shù)中找一對(duì)和是這個(gè)新目標(biāo)數(shù)的數(shù),其實(shí)就轉(zhuǎn)化為2Sum了。為了避免得到重復(fù)結(jié)果,我們不僅要跳過重復(fù)元素,而且要保證2Sum找的范圍要是在我們最先選定的那個(gè)數(shù)之后的。
代碼public class Solution { public List> threeSum(int[] nums) { Arrays.sort(nums); ArrayList
> res = new ArrayList
>(); for(int i = 0; i < nums.length - 2; i++){ // 跳過重復(fù)元素 if(i > 0 && nums[i] == nums[i-1]) continue; // 計(jì)算2Sum ArrayList
> curr = twoSum(nums, i, 0 - nums[i]); res.addAll(curr); } return res; } private ArrayList
> twoSum(int[] nums, int i, int target){ int left = i + 1, right = nums.length - 1; ArrayList
> res = new ArrayList
>(); while(left < right){ if(nums[left] + nums[right] == target){ ArrayList
curr = new ArrayList (); curr.add(nums[i]); curr.add(nums[left]); curr.add(nums[right]); res.add(curr); do { left++; }while(left < nums.length && nums[left] == nums[left-1]); do { right--; } while(right >= 0 && nums[right] == nums[right+1]); } else if (nums[left] + nums[right] > target){ right--; } else { left++; } } return res; } }
2019/01
Go
func threeSum(nums []int) [][]int { // sort the slice to avoid duplicate and also use two pointers sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] }) res := [][]int{} for i := 0; i < len(nums); i++ { // skip duplicate numbers if i != 0 && nums[i] == nums[i - 1] { continue } // convert into a twoSum problem res = twoSum(nums, i + 1, nums[i], res) } return res } func twoSum(nums []int, start, first int, res [][]int) [][]int { left := start right := len(nums) - 1 for left < right { sum := nums[left] + nums[right] if sum == 0 - first { res = append(res, []int{first, nums[left], nums[right]}) left++ right-- // skip duplicate numbers from left side for left < len(nums) && left > 0 && nums[left] == nums[left - 1] { left++ } // skip duplicate numbers from right side for right >= 0 && right < len(nums) - 1 && nums[right] == nums[right + 1] { right-- } } else if sum > 0 - first { right-- } else if sum < 0 - first { left++ } } return res }4Sum 雙指針法 復(fù)雜度
時(shí)間 O(N^3) 空間 O(1)
思路和3Sum的思路一樣,在計(jì)算4Sum時(shí)我們可以先選一個(gè)數(shù),然后在剩下的數(shù)中計(jì)算3Sum。而計(jì)算3Sum則同樣是先選一個(gè)數(shù),然后再剩下的數(shù)中計(jì)算2Sum。
代碼public class Solution { public List3Sum Closet> fourSum(int[] nums, int target) { Arrays.sort(nums); ArrayList
> res = new ArrayList
>(); for(int i = 0; i < nums.length - 3; i++){ if(i > 0 && nums[i] == nums[i-1]) continue; List
> curr = threeSum(nums, i, target - nums[i]); res.addAll(curr); } return res; } private List
> threeSum(int[] nums, int i, int target) { ArrayList
> res = new ArrayList
>(); for(int j = i + 1; j < nums.length - 2; j++){ if(j > i + 1 && nums[j] == nums[j-1]) continue; List
> curr = twoSum(nums, i, j, target - nums[j]); res.addAll(curr); } return res; } private ArrayList
> twoSum(int[] nums, int i, int j, int target){ int left = j + 1, right = nums.length - 1; ArrayList
> res = new ArrayList
>(); while(left < right){ if(nums[left] + nums[right] == target){ ArrayList
curr = new ArrayList (); curr.add(nums[i]); curr.add(nums[j]); curr.add(nums[left]); curr.add(nums[right]); res.add(curr); do { left++; }while(left < nums.length && nums[left] == nums[left-1]); do { right--; } while(right >= 0 && nums[right] == nums[right+1]); } else if (nums[left] + nums[right] > target){ right--; } else { left++; } } 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. You may assume that each input would have exactly one solution.雙指針法 復(fù)雜度For 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).
時(shí)間 O(N^2) 空間 O(1)
思路和3Sum的解法一樣。在3Sum中,我們只有找到和目標(biāo)完全一樣的時(shí)候才返回,但在Closet中,我們要記錄一個(gè)最小的差值,并同時(shí)記錄下這個(gè)最小差值所對(duì)應(yīng)的和。
代碼public class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int closetSum = 0, minDiff = Integer.MAX_VALUE / 2; for(int i = 0; i < nums.length; i++){ int left = i + 1, right = nums.length - 1; while(left < right){ // 當(dāng)前組合的和 int sum = nums[i] + nums[left] + nums[right]; // 當(dāng)前組合的和與目標(biāo)的差值 int diff = Math.abs(sum - target); // 如果差值更小則更新最接近的和 if(diff < minDiff){ closetSum = sum; minDiff = diff; } // 雙指針的移動(dòng)方法和3Sum一樣 if (sum < target){ left++; } else if (sum > target){ right--; } else { return sum; } } } return closetSum; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/64508.html
摘要:解題思路題目要求兩個(gè)數(shù)和等于,返回其題目說明不會(huì)有重復(fù)情況,所以我們一旦發(fā)現(xiàn)符合情況的,就可以直接結(jié)束循環(huán)并返回。特殊情況就是正好等于,那肯定是最接近的情況,直接返回即可。 Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific ta...
摘要:找符合條件的總數(shù)。雙指針區(qū)間考慮邊界,長(zhǎng)度,為空,等。之后的范圍用雙指針和表示。若三個(gè)指針的數(shù)字之和為,加入結(jié)果數(shù)組。不要求,所以不用判斷了。同理,頭部?jī)蓚€(gè)指針向后推移,后面建立左右指針夾逼,找到四指針和為目標(biāo)值的元素。 Two Sum Problem Given an array of integers, find two numbers such that they add up ...
摘要:題目詳情輸入一個(gè)長(zhǎng)度為的整數(shù)數(shù)組和一個(gè)目標(biāo)整數(shù),我們需要找出是否存在個(gè)元素,使得的和等于。如果有,輸出這樣的非重復(fù)的元素序列。在求元素的時(shí)候可以通過左右指針減少查找時(shí)間。 題目詳情 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target...
摘要:和方法一樣,多一個(gè)數(shù),故多一層循環(huán)。完全一致,不再贅述, 4Sum Problem 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 ...
摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的。可能大家看著也非常不方便。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的。可能大家看著也非常不方便。所以在這里做個(gè)索引嘻嘻。 順序整理 1~50 1...
閱讀 1000·2021-11-22 13:52
閱讀 1441·2021-11-19 09:40
閱讀 3122·2021-11-16 11:44
閱讀 1263·2021-11-15 11:39
閱讀 3893·2021-10-08 10:04
閱讀 5333·2021-09-22 14:57
閱讀 3096·2021-09-10 10:50
閱讀 3177·2021-08-17 10:13