LeetCode-兩數&三數之和系列問題
三數之和
LintCode57 三數之和
解題思路:先對陣列排序,然後開始遍歷,對於陣列中的每一個元素,用兩指標往中間夾,直到找出所有的解。時間複雜度 O(n^2).
ps:為什麼會想到對陣列元素進行排序呢,排序是為了讓元素之間呈現出某種規律,處理起來會簡單很多。所以,當你覺得一個似乎無從下手的問題的時候,不妨嘗試去尋找或製造一種“規律”,排序是手段之一。
public class Solution {
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Arrays.sort(numbers);
for (int i = 0; i < numbers.length; ++i) {
int l = i + 1, r = numbers.length - 1 ;
while (l < r) {
ArrayList<Integer> stepRes = new ArrayList<Integer>();
int sum = numbers[i] + numbers[l] + numbers[r];
if (sum == 0) {
stepRes.add(numbers[i]);
stepRes.add(numbers[l]);
stepRes.add(numbers[r]);
if (!res.contains(stepRes)) // 去重
res.add(stepRes);
}
if (sum <= 0) ++l;
else --r;
}
}
return res;
}
}
最接近的三數之和
public class Solution {
/**
* @param numbers: Give an array numbers of n integer
* @param target : An integer
* @return : return the sum of the three integers, the sum closest target.
*/
public int threeSumClosest(int[] numbers, int target) {
// if (numbers == null || numbers.length == 0) return 0;
int res = Integer.MAX_VALUE;
Arrays.sort(numbers);
for (int i = 0; i < numbers.length; ++i) {
int l = i + 1, r = numbers.length - 1;
while (l < r) {
int sum = numbers[i] + numbers[l] + numbers[r];
if (Math.abs(sum - target) < Math.abs(res - target)) res = sum;
if (sum <= target) ++l;
else --r;
}
}
return res;
}
}
兩數之和
LintCode56 兩數之和
解題思路:使用一個雜湊表記錄,比如說輸入資料 [2,7,11,15], 9
9 - 2 = 7, 讓 7 作為 key, 下標+1作為value, 這樣遍歷到陣列中的 7 時就找到解了
非嚴格情況下,雜湊表的查詢的插入操作都可以認為是在 O(1) 時間完成的,所以這個解法的時間複雜度為 O(n).
public class Solution {
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];
Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();
for (int i = 0; i < numbers.length; ++i) {
if (hash.containsKey(numbers[i])) {
res[0] = hash.get(numbers[i]);
res[1] = i + 1;
return res;
}
hash.put(target - numbers[i], i + 1);
}
return res;
}
}
注意:這裡不能使用類似以上兩題的解法,因為題目要求返回下標,而排序會丟棄陣列元素的下標資訊,因此不能排序。
相關推薦
LeetCode-兩數&三數之和系列問題
三數之和LintCode57 三數之和解題思路:先對陣列排序,然後開始遍歷,對於陣列中的每一個元素,用兩指標往中間夾,直到找出所有的解。時間複雜度 O(n^2).ps:為什麼會想到對陣列元素進行排序呢,排序是為了讓元素之間呈現出某種規律,處理起來會簡單很多。所以,當你覺得一個似乎無從下手的問題的時候,不妨嘗試
LeetCode 15 3sum 三數之和
題目連結 https://leetcode-cn.com/problems/3sum/ 題意 很簡單,就是給出一個數組,3個數一組,找到所有和為0的組。並且要求不能重複。或者說找其中3個數其和為0,找出所有的組合。 題解
LeetCode演算法題——三數之和
給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。 注意:答案中不可以包含重複的三元組。 例如, 給
Leetcode 923:三數之和的多種可能(最詳細的解法!!!)
給定一個整數陣列 A,以及一個整數 target 作為目標值,返回滿足 i < j < k 且 A[i] + A[j] + A[k] == target 的元組 i, j, k 的數量。 由於結果會非常大,請返回 結果除以 10^9 + 7 的餘數。 示例 1:
【LeetCode】15 三數之和3Sum
給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0? 找出所有滿足條件且不重複的三元組。 注意:答案中不可以包含重複的三元組。 例如, 給定陣列 nums = [-1, 0, 1, 2, -1, -4], 滿足
【LeetCode】#15三數之和(3Sum)
【LeetCode】#15三數之和(3Sum) 題目描述 給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。 注意:答案中不可以包含重複的三元組。 示例 例如, 給
[Leetcode] Python3 實現三數之和
給定一個包含 n 個整數的陣列 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重複的三元組。 注意:答案中不可以包含重複的三元組。 例如, 給定陣列 nums = [-1, 0, 1, 2, -1,
leetcode-15:3sum 三數之和
題目: Given an array nums of n integers, are there elements a, b, c in nums such that&n
LeetCode 15 — 3Sum(三數之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the arra
LeetCode之 15.三數之和 (3Sum)總結
生命不止,刷題不息~~~~~~ 前兩天就一直在做15.三數之和,這個題在LeetCode和LeetCode中國上獲贊很多,絕對的好題啊!不過,我喜歡這個題僅僅是因為它採用了快速排序的思想啦。 從捋清思路到程式碼實現,突破重重Bugs大關,終於提交成功,對於小白而言,實屬不
Leetcode刷題——三數之和
大噶猴,前一段比較忙,刷題日記被耽擱了一段時間,從今天起開始恢復。今天開始刷leetcode中等難度的演算法題了,第一道是三數之和,看下題目要求:思路:好久沒刷題了手非常生,思路也很枯竭,只想到了暴力迴圈,找到所有相加等於零的vector之後對每一個vector做一個排序然後
LeetCode(15. 三數之和)
sel sum 元組 app 判斷 print for .so 整數 問題描述 給定一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重復的三元組。 註意:答案中不可以包含重復的三
力扣(LeetCode)15. 三數之和
-- 整數 -c 去重 clas pub lis inf 分享 給定一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重復的三元組。 註意:答案中不可以包含重復的三元組。 例如,
LeetCode:兩數之和、三數之和、最接近的三數之和
都用雜湊表的方法 兩數之和 vector<int> twoSum(vector<int>& nums, int target) { int s;
[Leetcode]Two sum(兩數之和)系列總結
Two sum 題目 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume
LeetCode 15. 3Sum(三數之和)
while arr cnblogs 關鍵點 next 資料 () code find Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find
[LeetCode] 16. 3Sum Closest 最近三數之和
tor || sum max int public each href .com Given an array S of n integers, find three integers in S such that the sum is closest to a given
LeetCode 15. 三數之和(3Sum)
相加 mil pub 問題 push pan begin push_back 數組 題目描述 給定一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,使得 a + b + c = 0 ?找出所有滿足條件且不重復的三元組。 註意:
[leetcode]兩數之和
描述 () 另一個 -s nbsp get list 示例 class 題目描述: 給定一個整數數組和一個目標值,找出數組中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重復利用。 示例: 給定 nums = [2, 7, 11, 15], ta
[leetcode] 15. 三數之和
distinct c代碼 sent 循環 public 一點 fabs ava ray 此題相當惡心,多次超時 暴力是O(N^3),明顯不會過,不用考慮。 比較好一點的思路是,a+b = -c,我們可以考慮用hash表存下來每個數字是否出現以及出現的次數,枚舉a與b,然後看