441. Arranging Coins - Easy 前往題目 想法 循環扣除每行所需硬幣 思路 同想法 這題還能用binary search做,但反而就變困難了 CodeNeetcode大大用BS做的 class Solution { public int arrangeCoins(int n) { int i = 1; // row // Until used 2024-09-04 Leetcode > Easy #Leetcode #心得 #Math #Binary Search
341. Flatten Nested List Iterator 前往題目 想法 Queue or Stack 思路 用dfs,遇到數字就加入queue,遇到list就再呼叫dfs Code /** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its impl 2024-08-29 Leetcode > Medium #Leetcode #心得 #Stack #Queue #Depth-First Search #Tree #Design #Iterator
456. 132 Pattern - Medium 前往題目 想法 stack 思路挺難理解的一題,巧妙的運用monotonic stack 大致上是:利用monotonic stack,使推進去的數字遞減,只要當前數字比stack的頂端小,就pop,先當作132的2,因為3是當前數字,而下一個循環一開始就看當前是否比2還要小,如果是,就找到了 Code✅ 99.35% Stack & Left Approach & Binar 2024-08-28 Leetcode > Medium #Leetcode #心得 #Stack #Array #Monotonic Stack #Binary Search #Ordered Set
1209. Remove All Adjacent Duplicates in String II - Medium 前往題目 想法 Stack 思路 用Stack儲存每個字母和當前有幾個相鄰且相同的 達到k就pop 沒達到就計數+1然後繼續 相鄰的不是同字母計數=1 回傳結果 Code看了Hint自己寫出來了,insert耗費資源,用append最後再reverse就好 class Solution { public String removeDuplicates(String s, i 2024-08-21 Leetcode > Medium #Leetcode #心得 #Stack #String
402. Remove K Digits - Medium 前往題目 想法 Stack 思路Monotonic Stack,由小到大 疊代整個string 遇到當前數字比前一個小就彈出上一個數字(最多彈出k個) 還有k剩餘就彈出最後面的數字 回傳結果 要想到monotonic stack是這題的解法還真的有難度 Code class Solution { public String removeKdigits(String nu 2024-08-20 Leetcode > Medium #Leetcode #心得 #Stack #String #Monotonic Stack #Greedy
71. Simplify Path - Medium 前往題目 想法 stack 思路這題不難,不過edge case很多,要一次想出完美的條件判斷不容易 先用/分開 疊代分開後的每個部分 遇到.或是空的就跳過,因為不用做任何處理,直接忽略就好 遇到..就把上一個東西刪掉 其餘就都記錄下來 最後重新拼裝就好了 Codeclass Solution { public String simplifyPath(String pat 2024-08-16 Leetcode > Medium #Leetcode #心得 #Stack #String
901. Online Stock Span - Medium 前往題目 想法 Stack,但無從下手 思路 next()時,預設span為1 stack裡如果有<= price的,就pop然後加到當前span直到stack裡的數比當前price還要大。如此一來就可以捨去一些數字,因為span都儲存在後來的數字裡 結束前把新的price以及span推進stack 變成Monotonic decreasing的stack Code class St 2024-08-15 Leetcode > Medium #Leetcode #心得 #Stack #Monotonic Stack #Design #Data Stream
946. Validate Stack Sequences - Medium 前往題目 想法 stack 疊代pushed的部分,去匹配popped的元素 思路 循環pushed 每次循環都看stack的頂端是否和popped的元素相符 相符就pop,不相符就繼續 最後看stack是否為空判斷 Code class Solution { public boolean validateStackSequences(int[] pushed, int[ 2024-08-06 Leetcode > Medium #Leetcode #心得 #Stack #Array #Simulation
2390. Removing Stars From a String - Medium 前往題目 想法 Stack 思路這題很簡單遇到米字號直接pop Codeclass Solution { public String removeStars(String s) { Stack<Character> stack = new Stack(); for (char c : s.toCharArray()) 2024-08-06 Leetcode > Medium #Leetcode #心得 #Stack #String #Simulation
1544. Make The String Great - Easy 前往題目 想法 Stack 思路 循環每個字符 找到bad string時,彈出,其他都push到stack 建構答案並回傳 Codeclass Solution { public String makeGood(String s) { Stack<Character> stack = new Stack<>(); / 2024-08-05 Leetcode > Easy #Leetcode #心得 #Stack #String