[Leetcode] 15. 3Sum

閱讀時間約 5 分鐘

題目 : 15. 3Sum Medium

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != ji != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.


Example 1:

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.

Example 2:

Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.

Example 3:

Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.


1.

這一題是3個數字相加=0,所以我們只要固定第1個數字,把它當成目標數,利用迴圈讓目標數從目標數之後的最左邊(l)和最右邊(r)的數根據3個數相加的結果來判斷要將l往右走或是r往左走來縮小範圍。

目標 l-> <-r

-1 , -1 , 0 , 1 , 2 , 3

(當目標是-1的答案 : [-1,-1,2]、[-1,0,1])


line 5 : 為了加速找數字的時間和方便判別是否有遇過重複的數字,所以將nums用sorts()進行排序

line 8 : 如果目前的num和前一個數字是一樣的,就直接continue下一個num,這是要避免掉重複的答案

line 11 : l: 是目標數之後最左邊的index,r : 是整個List中最右邊的index

line 13 : 如果threeSum3個數字相加>0,代表數字太大,要將r ​往左縮小範圍,讓threeSum的數值變小

line 16 : 如果threeSum3個數字相加<0,代表數字太小,要將l ​往右縮小範圍,讓threeSum的數值變大

line 18 : 如果threeSum3個數字相加=0,代表得到答案!

line 19 : 答案append到res中

line 21 : 接下來可以看下一個l,為了避免重複的答案,我們要判斷l跟上一個l-1是不是一樣,如果遇到一樣的情況l再加1

 class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:

res = []
nums.sort()

for i, num in enumerate(nums):
if i > 0 and num == nums[i-1]:
continue

l, r = i+1, len(nums)-1
while l < r:
threeSum = num + nums[l] + nums[r]
if threeSum > 0 :
r -= 1
elif threeSum < 0 :
l += 1
else :
res.append([num,nums[l],nums[r]])
l += 1
while l < r and nums[l] == nums[l-1]:
l += 1
return res







7會員
47內容數
這裡會放一些我寫過的 Leetcode 解題和學習新技術的筆記
留言0
查看全部
發表第一個留言支持創作者!
Youna's Devlog 的其他內容
[Leetcode] 49. Group Anagrams
閱讀時間約 3 分鐘
[Leetcode] 125. Valid Palindrome
閱讀時間約 3 分鐘
[Leetcode] 20. Valid Parentheses
閱讀時間約 3 分鐘
你可能也想看
應用題 從阿拉伯數字轉成羅馬數字 Leetcode 12: Integer to Roman題目會給定我們一組十進位表示法的阿拉伯數字,要求我們把它轉換為對應的羅馬數字。
Thumbnail
avatar
小松鼠
2023-10-15
【LeetCode】946. Validate Stack Sequences 題目 Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop
Thumbnail
avatar
2023-04-15
【LeetCode】876. Middle of the Linked ListInput: head = [1,2,3,4,5] Output: [3,4,5] 單看列表只是要找中間值,不過給定的資料結構不是陣列,而是鏈結串列。
Thumbnail
avatar
2021-10-09
【LeetCode】189. Rotate Array之前跳過的題目,回來補完成。 Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4]
Thumbnail
avatar
2021-10-08
【LeetCode】557. Reverse Words in a String III今日題目: 把一行字內每個單字都反轉字元。 Input: s = "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
Thumbnail
avatar
2021-10-08
【LeetCode】344. Reverse String今日題目:字串反轉 Input: s = ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
Thumbnail
avatar
2021-10-08
【LeetCode】167. Two Sum II 題目如下: Input: numbers = [2,7,11,15], target = 9 Output: [1,2]
Thumbnail
avatar
2021-10-07
【LeetCode】283. Move Zeroes題目要求如下: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 把0都搬到後面去,非0的數字移到前面,且不更改原本數字的大小順序。
Thumbnail
avatar
2021-10-07
【LeetCode】704. Binary SearchLeetcode上正好有14天的讀書計畫,今天三題都是二分搜索,順手三題都寫一寫,想法上很固定。
Thumbnail
avatar
2021-10-05
【LeetCode】20. Valid Parentheses 前幾天看別人直播刷題,心血來潮打開很久沒動的leetcode試試,挑了一題當初面試沒寫出來的題目。嗯...我那時才剛碰資料結構,知道要用stack寫卻沒實作過任何東西,現在有工作經驗後再來寫,看看會不會有不一樣的想法。
Thumbnail
avatar
2021-10-04