2022-02-24 解題區►Leetcode►Easy Leetcode 169 - Majority Element 題目 Problem# 題目給你一個陣列 nums,回傳出現次數超過 $\lfloor \frac{n}{2} \rfloor$ 的的那個數字(眾數)。 想法# 直覺做就是先建表紀錄每個數字出現的次數,接著排序拿最大即可 Read More
2022-02-23 解題區►Leetcode►Medium Leetcode 133 - Clone Graph 題目 Problem# 給你一張圖,要你 Deep copy 一份並回傳。 想法# BFS 遍歷一次,邊走邊複製。 Read More
2022-02-22 解題區►Leetcode►Easy Leetcode 171 - Excel Sheet Column Number 題目 Problem# 題目給你一個字串 S 由大寫英文字母組成,令 A=1, B=2, ..., Z=26, AA=27, AB=28, ... ,要你算出並回傳數值。 想法# 照做即可 Read More
2022-02-20 解題區►Leetcode►Medium Leetcode 1288 - Remove Covered Intervals 題目 Problem# 題目給你一個由正整數構成的區間,intervals[i] = [li, ri],如果區間 [a, b) 被 [c, d) 覆蓋,則代表 $c <= a$ 且 $b <= d$ 要你回傳刪掉被覆蓋的區間後,剩下區間的個數。 N = intervals.size() $1 \leq N \leq 1000$ 區間範圍: $0 \leq l_i \leq r_i \leq 10^5$ 想法# Read More
2022-02-20 解題區►Leetcode►Hard Leetcode 1675 - Minimize Deviation in Array 題目 Problem# 題目給你一列正整數 N ,你可以對數列中的每個元素執行兩種操作: 如果 N[i] 是偶數,可以除 2 如果 N[i] 是奇數,可以乘 2 定義 deviation 是數列中的最大和最小之差 max(N) - min(N) 問你經過幾次操作後,最小的 deviation 是多少? Read More
2022-02-20 解題區►Leetcode►Hard Leetcode 127 - Word Ladder 題目 Problem# 一個字串能夠從 beginWord 轉換成 endWord 稱作 transformation sequence e.g. beginWord -> s1 -> s2 -> ... -> sk,今天題目給你一個 wordList 問你從 beginWord 到 endWord 總共幾個字串,並且要最短的。 其中每個字串只差一個字元 => 相差一個字元才能轉換 轉換的字串都在 wordList 裏頭 beginWord 不一定要在 wordList 裏頭 想法# Read More
2022-02-19 解題區►Leetcode►Medium Leetcode 39 - Combination Sum 題目 Problem# 給你一組數字 candidates 和一個數字 target ,問你能用candidates中的元素組成不重複的組合,且相加要等於 T。相同的數字可以出現無限次 1 <= candidates.size() <= 30 1 <= candidates[i] <= 200 1 <= T <= 500 想法# Read More
2022-02-19 解題區►Leetcode►Medium Leetcode 402 - Remove K Digits 題目 Problem# 給你一個只有非負整數的字串 num 和整數 k,要你回傳刪掉 k 個數字後,之中最小的整數 123Input: num = "1432219", k = 3Output: "1219"Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest. 想法# Read More
2022-02-17 解題區►Leetcode►Easy Leetcode 104 - Maximum Depth of Binary Tree 題目 Problem# 給一個二元樹,要你回傳樹高 想法# DFS 遍歷整顆樹,可用 backtracking 到 leaf 時紀錄走了多深,找最大即可 遍歷時可拆成,max( dfs(左子樹), dfs(右子樹) ) Read More
2022-02-16 解題區►Leetcode►Medium Leetcode 24 - Swap Nodes in Pairs 題目 Problem# 題目給一個 Singly linked list 要你把每兩個兩個 swap 一遍,但不能只 swap value 想法# 照做即可,注意邊界 e.g. list 沒有 node 的狀態、list 只有一個 node 的狀態 Read More