Simon Lai's Blog
  • 首頁
  • 歸檔
  • 分類
  • 標籤
  • 關於

131. Palindrome Partitioning - Medium

前往題目 想法 backtracking把所有組合都看過,每次要判斷是否是palindrome然後才加入 思路 平常的backtrack 要紀錄起始點,然後每個循環都要判斷是否為palindrome Codeclass Solution { List<List<String>> res = new LinkedList<>(); LinkedLis
2024-02-13
Leetcode > Medium
#Leetcode #心得 #String #Dynamic Programming #Backtracking

40. Combination Sum II - Medium

前往題目 想法 和今天稍早寫的另一題(90)一樣的套路 思路 先sort,這樣才好跳過取過的數字 正常backtrack 唯一需要注意的是追蹤index,如果循環中的index已經比傳入的index還大的時候就要判斷是否和前一個數字一樣,要跳過,代表前一個已經被取過了 這題很妙的是和第90一樣的做法,但90是不能取同一個subset即便順序不同,這個是不能取到取過的數字 總之這個技巧的核心就
2024-02-12
Leetcode > Medium
#Leetcode #心得 #Array #Backtracking

90. Subsets II - Medium

前往題目 想法 以為是簡單的backtrack,但是需要考慮如何防止加入重複的subset 思路 先sort,這樣才好判斷相同的數字 正常backtrack 唯一需要注意的是追蹤index,如果循環中的index已經比傳入的index還大的時候就要判斷是否和前一個數字一樣,要跳過 判斷的邏輯很妙,尤其是i > idx,後來想了想應該是i == idx的時候不用判斷,因為還沒加入,加入後
2024-02-12
Leetcode > Medium
#Leetcode #心得 #Array #Bit Manipulation #Backtracking

46. Permutations - Medium

前往題目 想法 之前做過的簡單backtracking 思路 backtracking check是否數字已存在,以免重複加入 暫存的list可能可以用其他實作方式,可以removeLast in O(1),不然arraylist要移除最後一項需要N次操作 Codeclass Solution { List<List<Integer>> res; pub
2024-02-11
Leetcode > Medium
#Leetcode #心得 #Array #Backtracking

33. Search in Rotated Sorted Array - Medium

前往題目 想法 之前寫過,條件實在不好想 思路 Binary search整個nums 如果mid不是答案,看左指針的值是否小於等於mid的值,代表順序正常,再接著判斷,是否左邊的portion被rotate到右邊了 如果都不是那判斷右邊portion是否被rotate到左邊去了,並移動指針 這次第二次做,覺得可以思考的方向是: mid和l指針的值正常的情況(l <= mid)如何判斷
2024-02-11
Leetcode > Medium
#Leetcode #心得 #Array #Binary Search

39. Combination Sum - Medium

前往題目 想法 之前寫過,要用backtracking,試試這次能不能自己寫出來 思路就差臨門一腳,沒注意到index需要被pass,不然每次都會從0開始,結果雖然都是對的但會有重複項 backtracking 疊代所有數字,每次都傳新的sum以及當前index,如果如果sum等於target就加入,大於就直接return 每次循環結束前都要移除最後一項,因為已經檢查過了 Codeclas
2024-02-10
Leetcode > Medium
#Leetcode #心得 #Array #Backtracking

355. Design Twitter - Medium

前往題目 想法 卡在getNewsFeed 思路OOD方式 定義Tweet物件和User物件,包含各種訊息 postTweet直接用id取得用戶物件然後post getNewsFeed一開始先檢查userId是否存在,接著疊代該用戶追蹤的用戶們,取得他們最新的tweet,然後第二個循環就開始取得10個最新tweet,先加入最新的tweet,然後同時也加入該tweet的下一個,這樣一直循環直到
2024-02-10
Leetcode > Medium
#Leetcode #心得 #Hash Table #Linked List #Heap (Priority Queue) #Design

1046. Last Stone Weight - Easy

前往題目 想法 用priority queue 思路這題無需考慮位置,因為最終回傳的也是剩下的weight,而不是哪顆石頭,所以非常簡單 利用priority queue把stones存為descending order(降冪) 然後把前兩項相減後的結果再加入pq(兩數都要poll) 直到pq.size只剩1,直接回傳pq.poll() 題目說如果沒有石頭那就回傳0,而我沒判斷也AC,應該
2024-02-08
Leetcode > Easy
#Leetcode #心得 #Array #Heap (Priority Queue)

703. Kth Largest Element in a Stream - Easy

前往題目 想法 用priority queue 思路 把初始array放進priority queue裡面 每次加入的時候都先加入新的數字,然後再把pq刪減到剩下k項(因為這題只有add方法所以只要維持k個就好,因為k值固定所以更小的數字永遠不會用到) Codeclass KthLargest { private int k; private PriorityQue
2024-02-08
Leetcode > Medium
#Leetcode #心得 #Binary Search Tree #Binary Tree #Tree #Heap (Priority Queue) #Design #Data Stream

1448. Count Good Nodes in Binary Tree - Medium

前往題目 想法 搜尋的時候維護最大值 思路難得自己寫出來DFS DFS 搜尋時維護最大值 Codeclass Solution { private int res; // Result public int goodNodes(TreeNode root) { res = 0; // Start DFS d
2024-02-07
Leetcode > Medium
#Leetcode #心得 #Binary Tree #Depth-First Search #Breadth-First Search #Tree
1…2021222324…36

搜尋

Hexo Fluid