發表文章

LeetCode 解題練習:Plus One

題目原文描述  https://leetcode.com/problems/plus-one/ 中文描述 給定一個用陣列 digits 表示大整數 large integer,digits[i] 代表大整數的第 i 位數之值。例如 12345 會以 digits = [1, 2, 3, 4, 5] 來表示。請將 digits 的數值加一。 範例一: 輸入 digits = [9,9,9,9]  輸出  [1, 0, 0, 0, 0] 說明:整數 9999  加一後為 10000。 範例二: 輸入 digits = [2, 2, 3, 4, 5]  輸出  [2, 2, 3, 4, 6] 說明:整數 22345  加一後為 22346。 解法: 這是一題非常經典的 LeetCode 基礎題(Plus One)。這題的核心考點在於「如何處理進位(Carry)」,特別是當連續出現數字 9 ,或是整個陣列都是 9 的極端情況(Corner Case)。 以下為您將這個演算法的邏輯、步驟與程式碼做更深入、詳細的拆解與說明: 💡 核心解題邏輯 我們從小學數學的直式加法可以知道,加 1 都是從 最右邊的個位數 開始加。這時候會遇到三種情況: 普通情況(無進位) :最右邊的數不是 9(例如: [1, 2, 3] )。我們直接把個位數加 1 變成 [1, 2, 4] ,任務就結束了。 部分進位 :結尾有連續的 9(例如: [1, 2, 9] )。個位數的 9 加 1 變成 10,必須 進位 。所以個位數變成 0 ,再往前看十位數的 2 ,它不是 9,所以加 1 變成 3 。結果為 [1, 3, 0] ,任務結束。 全數進位(極端情況) :所有數字都是 9(例如: [9, 9, 9] )。每個位置都會因為進位變成 0 ,最後陣列會變成 [0, 0, 0] 。這時候我們必須在 最左邊手動補一個 1 ,讓它變成 [1, 0, 0, 0] 。 注意:若目前 digits[i] 為 9 ,將 digits[i] 設定為 0;否則 digits[i] 加一並回傳 digits。若每位數都是9,在最左邊補1。 🛠️ Python 程式碼 class Solution : def plusOne ( self, d...

LeetCode 解題:Find Pivot Index

圖片
題目原文描述  https://leetcode.com/problems/find-pivot-index/ 中文描述 給定一個整數陣列 nums ,找出某目標索引位置 Pivot Index,讓目標索引位置的左邊元素陣列元素總和等於目標索引位置的右邊陣列元素總和,總和不包含目標索引位置之值。 範例一: 輸入 nums = [1, 2, 3, 4, 2, 2, 2]  輸出  3 因為  [1, 2, 3] 總和等於 6 等於 [2, 2, 2]總和 範例二: 輸入 nums = [3, -3, 3]  輸出  0 因為  [] 總和等於 0 等於 [-3, 3] 總和 解法一: 暴力法。對每一個索引位置算出左邊元素陣列元素總和 leftSum 與右邊元素陣列元素總和 rightSum 是否相等。 Python Code class Solution :     def pivotIndex ( self , nums : List[ int ]) -> int :         total = sum (nums) # 陣列總和         leftSum = 0 # 左邊總和         for i in range ( len (nums)): # 從左邊索引0開始找起             rightSum = sum (nums[i+ 1 :]) # 右邊總和             leftSum = total - rightSum - nums[i] # 左邊總和             if leftSum == rightSum: # 若一樣                 return i # 找到 Pivot Index     ...

LeetCode 解題練習:Find All Numbers Disappeared in an Array

圖片
題目原文描述  https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 中文描述 給定一個含有[1, n] 之間的n個元素之整數陣列 nums (陣列長度為 n),找出[1, n] 之間沒有出現在 陣列 nums 元素中的整數。  範例一: 輸入 nums = [2, 2]  輸出 [1] 範例二: 輸入 nums = [1, 2, 2]  輸出 [3] 範例三: 輸入 nums = [1, 2, 1, 2, 1, 3, 4]  輸出 [5, 6, 7] 解法一: 使用計數的方式記錄[1,n]每一個數字出現在 nums 的次數,再統計出現次數為0的數字有哪些。 Python Code class Solution :     def findDisappearedNumbers ( self , nums : List[ int ]) -> List[ int ]:         n = len (nums)         counts = [ 0 ] * n # 計算 [1,n] 數字出現的次數                 for i in nums:             counts[i - 1 ] += 1 # 出現一次就增加1                 missingNumbers = [] # 沒出現在 nums 的數字                 for i in range (n):             if counts[i] == 0 : # 若出現次數為0次         ...

LeetCode 解題練習:Height Checker

題目原文描述  https://leetcode.com/problems/height-checker/   中文描述 給定一個整數陣列 heights ,請算出 heights 中沒有依照由小到大之順序的元素個數。 範例一: 輸入 heights = [3, 1, 2]  輸出 3 因為依照小到大的陣列為 [1, 2, 3] ,heights 每個元素皆不再所屬的排序位置上。 範例二: 輸入 heights = [1, 3, 2]  輸出 2 因為依照小到大的陣列為 [1, 2, 3] ,heights[0]有在排序的位置上,其餘元素皆不在所屬的排序位置上。 範例三: 輸入 heights = [1, 2, 3]  輸出 0 因為依照小到大的陣列為 [1, 2, 3] ,heights所有元素皆在所屬的排序位置上。 解法: 先用內建排序函式產生由小排到大的 expected 陣列,依照索引位置 i 算出 heights[i] != expected[i] 有多少個。 Python Code class Solution :     def heightChecker ( self , heights : List[ int ]) -> int :         expected = sorted (heights)         notMatch = 0         for i in range ( len (heights)):             if heights[i] != expected[i]:                 notMatch += 1         return notMatch 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on ...

LeetCode 解題練習:Sort Array By Parity

圖片
題目原文描述  https://leetcode.com/problems/sort-array-by-parity/ 中文描述 給定一個整數陣列 nums ,產生一個陣列將nums中所有偶數放在陣列開頭,放完偶數後再放奇數。 範例一: 輸入 nums = [5, 2, 4, 1]  輸出 [2, 4, 5, 1] 或 [4, 2, 1, 5] 或 [2, 4, 1, 5] 或 [4, 2, 5, 1] 皆可為答案 範例二: 輸入 nums = [5, 2, 1]  輸出 [2, 5, 1] 或 [2, 1, 5] 皆可為答案 解法一: 建立一個陣列 parityArr ,迴圈走訪 nums 陣列,若 nums[i] 為偶數,加到 parityArr 最前面;若 nums[i] 為奇數,加到 parityArr 最後面。 Python Code class Solution :     def sortArrayByParity ( self , nums : List[ int ]) -> List[ int ]:         parityArr = []                 for n in nums:             if n % 2 == 0 :                 parityArr.insert( 0 , n) # 偶數加到parityArr最前面             else :                 parityArr.append(n) # 奇數加到parityArr最後面         return parityArr 解法二: 同解法一,但用 Python List Comprehension ...

LeetCode 解題練習:Move Zeroes

圖片
題目原文描述  https://leetcode.com/problems/move-zeroes/ 中文描述 給定一個整數陣列 nums ,將所有的 0 移到陣列結尾處,並保持原先非零整數的順序。請修改原本陣列 nums 的內容來完成。 範例一: 輸入 nums = [0, 1, 0, 5, 0, 0, 3]  輸出 nums = [1, 5, 3, 0, 0, 0, 0] 範例二: 輸入 nums = [0, 0, 1]  輸出 nums = [1, 0, 0] 範例三: 輸入 nums = [1]  輸出 nums = [1] 範例四: 輸入 nums = [0]  輸出 nums = [0] 解法一: left = 0,從左邊開始往右移動,如果陣列元素 nums[i] 不等於零 ,則將此元素複製到 nums[left],再將 left 加 1。走訪完陣列後,left 為非零整數的數量,從left索引位置之後的元素通通設為零。 Python Code class Solution :     def moveZeroes ( self , nums : List[ int ]) -> None :         """         Do not return anything, modify nums in-place instead.         """         left = 0         for i in range ( len (nums)): # 從左邊開始往右移動             if nums[i] != 0 : # 陣列元素 nums[i] 不等於零                 nums[left] = nums[i] # 將此元素複製到 nums[left]     ...

LeetCode 解題練習:Replace Elements with Greatest Element on Right Side

圖片
題目原文描述  https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/ 中文描述 給定一個整數陣列 arr,將每個元素用該元素的右邊所有元素中之最大值來取代,並將最後一個元素用-1取代。 範例一: 輸入  arr = [45, 333, 2, 1, 9, 17] 輸出 [333, 17, 17, 17, 17, -1] 因為 45 右邊元素 [333, 2, 1, 9, 17] 最大值為 333。 因為 333 右邊元素 [2, 1, 9, 17] 最大值為 17。 因為 2 右邊元素 [1, 9, 17] 最大值為 17。 因為 1 右邊元素 [9, 17] 最大值為 17。 因為 9 右邊元素 [17] 最大值為 17。 範例二: 輸入  arr = [333] 輸出 [-1]   解法: 以 curMax = -1 當目前最大值,t暫存目前陣列元素。從陣列的右邊開始以curMax替代,若 t 大於 curMax,則更新 curMax 為 t。 可參考底下圖片動畫說明: Python Code class Solution :     def replaceElements ( self , arr : List[ int ]) -> List[ int ]:         curMax = - 1 # 目前最大值         for i in range ( len (arr)- 1 , - 1 , - 1 ): # 從右邊開始替代             t = arr[i] # 暫存目前陣列元素             arr[i] = curMax # 以目前最大值替代陣列元素             if t > curMax: # 若此陣列元素比目前最大值大      ...