【leetcode】#陣列【Python】16. 3Sum Closest 最接近的三數之和
連結:
題目:
給定一個包括 n 個整數的陣列 nums 和 一個目標值 target。找出 nums 中的三個整數,使得它們的和與 target 最接近。返回這三個數的和。假定每組輸入只存在唯一答案。
例如,給定陣列 nums = [-1,2,1,-4], 和 target = 1.
與 target 最接近的三個數的和為 2. (-1 + 2 + 1 = 2).
class Solution:
def threeSumClosest(self, nums, target):
closest = 1000
closesum = 0
nums = sorted(nums)
for i in range(len(nums)-2):
if i>0 and nums[i] == nums[i-1]:
continue
j = i + 1
k = len(nums) - 1
while j<k:
if j-1 != i and nums[j] == nums[j-1]:
j += 1
continue
if nums[i]+nums[j]+nums[k]-target == 0:
closesum = nums[i]+nums[j]+nums[k]
return closesum
elif nums[i]+nums[j]+nums[k]-target < 0:
if abs(nums[i]+nums[j]+nums[k]-target) < closest:
closest = abs(nums[i]+nums[j]+nums[k]-target)
closesum = nums[i]+nums[j]+nums[k]
j += 1
elif nums[i]+nums[j]+nums[k]-target > 0:
if abs(nums[i]+nums[j]+nums[k]-target) < closest:
closest = abs(nums[i]+nums[j]+nums[k]-target)
closesum = nums[i]+nums[j]+nums[k]
k -= 1
return closesum
別人的解法1:
class Solution:
def threeSumClosest(self, nums, target):
nums = sorted(nums)
#res表示最小的三數和
res = sum(nums[:3])
for i in range(len(nums)-2):
j = i + 1
k = len(nums) - 1
while j<k:
temp = nums[i] + nums[j] +nums[k]
#更新res
if abs(res - target) > abs(temp-target):
res = temp
if target < temp:
k-=1
else:
j+=1
return res
看了別人的解法後,對我的解法做優化:80ms,擊敗83%
class Solution:
def threeSumClosest(self, nums, target):
closesum = sum(nums[:3])
nums = sorted(nums)
for i in range(len(nums)-2):
if i>0 and nums[i] == nums[i-1]:
continue
j = i + 1
k = len(nums) - 1
while j < k:
if j-1 != i and nums[j] == nums[j-1]:
j += 1
continue
temp = nums[i]+nums[j]+nums[k]
if temp-target == 0:
return temp
#把這個if抽出來了
if abs(temp-target) < abs(closesum-target):
closesum = temp
if temp-target < 0:
j += 1
elif temp-target > 0:
k -= 1
return closesum
相關推薦
leetcode-16:3sum closest最近的三數之和
題目: Given an array nums of n integers and an integer target, find three integers in nums such that t
【leetcode】#陣列【Python】16. 3Sum Closest 最接近的三數之和
連結: 題目: 給定一個包括 n 個整數的陣列 nums 和 一個目標值 target。找出 nums 中的三個整數,使得它們的和與 target 最接近。返回這三個數的和。假定每組輸入只存在唯一答
[LeetCode]16. 3Sum Closest最接近的三數之和
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the su
LeetCode 16 — 3Sum Closest(最接近的三數之和)
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return
[LeetCode] 3Sum Closest 最近的三數之和 Python
3Sum Closest: 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
LeetCode 3Sum Closest 最接近目標數的三個數和
3Sum Closest Given an arraySofnintegers, find three integers inSsuch that the sum is closest t
【leetcode】#陣列【Python】26. Remove Duplicates from Sorted Array 刪除排序陣列中的重複項
題目: 給定一個排序陣列,你需要在原地刪除重複出現的元素,使得每個元素只出現一次,返回移除後陣列的新長度。 不要使用額外的陣列空間,你必須在原地修改輸入陣列並在使用 O(1) 額外空間的條件下完成。
【leetcode】#陣列【Python】88. Merge Sorted Array 合併兩個有序陣列
題目: 給定兩個有序整數陣列 nums1 和 nums2,將 nums2 合併到 nums1 中,使得 num1 成為一個有序陣列。 說明: 初始化 nums1 和 nums2 的元素數量分別為 m
【leetcode】#陣列【Python】122. Best Time to Buy and Sell Stock II 買賣股票的最佳時機
連結: 題目: 給定一個數組,它的第 i 個元素是一支給定股票第 i 天的價格。 設計一個演算法來計算你所能獲取的最大利潤。你可以儘可能地完成更多的交易(多次買賣一支股票)。 注意:你不能同時參與多
【leetcode】#陣列【Python】48. Rotate Image 旋轉影象
連結: 題目: 給定一個 n × n 的二維矩陣表示一個影象。 將影象順時針旋轉 90 度。 說明: 你必須在原地旋轉影象,這意味著你需要直接修改輸入的二維矩陣。請不要使用另一個矩陣來旋轉影象。 示
【leetcode】#陣列【Python】59. Spiral Matrix II螺旋矩陣 II
連結: 題目: 給定一個正整數 n,生成一個包含 1 到 n2 所有元素,且元素按順時針順序螺旋排列的正方形矩陣。 示例: 輸入: 3 輸出: [ [ 1, 2, 3 ], [ 8, 9, 4
【leetcode】#陣列【Python】64. Minimum Path Sum 最小路徑和
連結: 題目: 給定一個包含非負整數的 m x n 網格,請找出一條從左上角到右下角的路徑,使得路徑上的數字總和為最小。 說明:每次只能向下或者向右移動一步。 示例: 輸入: [ [1,3,
【leetcode】#陣列【Python】74. Search a 2D Matrix 搜尋二維矩陣
連結: 題目: 編寫一個高效的演算法來判斷 m x n 矩陣中,是否存在一個目標值。該矩陣具有如下特性: 每行中的整數從左到右按升序排列。 每行的第一個整數大於前一行的最後一個整數。 示例 1:
【leetcode】#陣列【Python】80. Remove Duplicates from Sorted Array II 刪除排序陣列中的重複項 II 雙指標
連結: 題目: 給定一個排序陣列,你需要在原地刪除重複出現的元素,使得每個元素最多出現兩次,返回移除後陣列的新長度。 不要使用額外的陣列空間,你必須在原地修改輸入陣列並在使用 O(1) 額外空間
【leetcode】#陣列【Python】120. Triangle 三角形最小路徑和
連結: 題目: 給定一個三角形,找出自頂向下的最小路徑和。每一步只能移動到下一行中相鄰的結點上。 例如,給定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自頂向下的最小路徑和為 11(即,2
【leetcode】#陣列【Python】13. Roman to Integer 羅馬數字轉整數
連結: 題目: 給定一個羅馬數字,將其轉換成整數。輸入確保在 1 到 3999 的範圍內。 示例 1: 輸入: “III” 輸出: 3 示例 2: 輸入: “IV” 輸出: 4 示例 3: 輸入
【LeetCode】16. 3Sum Closest - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given an array nums of n integers and an integer target, find three inte
【leetcode】16 3Sum Closest
描述 給定一個數字集合 S 以及一個數字 target,需要從集合中找出3個數字的和與這個 target的值最接近(絕對值最小) 樣例 Input: S = [-1, 2, 1, -4], target = 1 Output: 2 思路 首先排序,之後確定一個數字的前提下,再利用雙指標從兩端
【LeetCode】#16最接近的三數之和(3Sum Closest)
【LeetCode】#16最接近的三數之和(3Sum Closest) 題目描述 給定一個包括 n 個整數的陣列 nums 和 一個目標值 target。找出 nums 中的三個整數,使得它們的和與 target 最接近。返回這三個數的和。假定每組輸入只存在唯一答案。 示例
【LeetCode】16. 3Sum Closest(C++)
題目: Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. R