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

225. Implement Stack using Queues - Easy

前往題目 想法 用兩個queue,雖然題目也有寫 思路這題很簡單,注意前後順序就好 push: 看哪個queue不是空的就放哪個pop: 看哪個是空的,把另一個queue的元素搬過來,最後剩一個直接回傳top: 一樣看哪個是空的,搬過去,剩下最後一個記錄下來回傳,也要辦過去另一個queueempty: 看兩個queue是否都為空 只用一個queue的反而更簡單,只要每次push的時候都反轉使其
2024-08-02
Leetcode > Easy
#Leetcode #心得 #Stack #Queue #Design

682. Baseball Game - Easy

前往題目 想法 Stack 思路 疊代所有操作 每次根據其操作來更新stack(遇到+直接退出前兩個加起來再重新入stack) 彈出所有數字算出總和 Codeclass Solution { public int calPoints(String[] operations) { Stack<Integer> s = new Stack();
2024-08-01
Leetcode > Easy
#Leetcode #心得 #Stack #Array #Simulation

Fluid VSCode Extension-一款為Hexo框架Fluid主題打造的擴展

介紹這個擴展是為使用Fluid主題的Hexo用戶量身定製的,提供自動語句片段貼上功能 功能透過輸入和選擇命令來貼上語句 使用方法 從market安裝,或是vscode裡面直接搜尋Hexo Snippet Paste Tool for Fluid安裝 F1打開命令面板 輸入Fluid Paste Tool,基本上不需要全部輸入就會顯示這個擴展的命令,輸入幾個字符後應該會看到如下所示的命令列表 所有可
2024-07-30
Project > VSCode
#Fluid #Hexo #Plugin #擴展 #VSCode

1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold - Medium

前往題目 想法 Sliding window 思路簡單的那種固定window size的 每次循環都更新總和 檢查平均,如果滿足threshold加入結果 循環結束前都移動左指針並且從sum減去相對應的數字 Codeclass Solution { public int numOfSubarrays(int[] arr, int k, int threshold) 
2024-07-30
Leetcode > Medium
#Leetcode #心得 #Array #Sliding Window

2958. Length of Longest Subarray With at Most K Frequency - Medium

前往題目 想法 經典Sliding window 思路 左右指針 每次循環移動右指針,更新右指針數字的數量 如果發現invalid window移動左指針直到window為valid 更新window的大小 這次完整的自己寫出來一次AC,而且花不到兩分鐘👍 Code class Solution { public int maxSubarrayLength(int[]
2024-07-29
Leetcode > Medium
#Leetcode #心得 #Array #Hash Table #Sliding Window

713. Subarray Product Less Than K - Medium

前往題目 想法 用標準的Sliding window會漏掉幾個沒算到 思路關鍵點只有在update的時候要取window size,這樣才是當前這個window的subarray總數 例如[5, 2, 6],window size是3,也代表他有三個subarray -> [5], [5, 2], [5, 2, 6] 標準sliding window,左右指針 每次循環更新當前prod
2024-07-26
Leetcode > Medium
#Leetcode #心得 #Array #Sliding Window

930. Binary Subarrays With Sum - Medium

前往題目 想法 Sliding window 但是全0的case不知道該怎解 思路這題最重要的是找出<= goal以及<= goal - 1總共有幾個valid window相減,如此一來就能找出== goal的有幾個window了 helper方法回傳goal減去goal - 1的結果 helper就是標準的sliding window程式 Code class Solut
2024-07-24
Leetcode > Medium
#Leetcode #心得 #Array #Hash Table #Sliding Window #Prefix Sum

1208. Get Equal Substrings Within Budget - Medium

前往題目 想法 把string按照ascii排序 雙sliding window把結果找出來 思路我的想法太多餘了,其實就是簡單的sliding window sliding window 每次循環更新當前總和 超過maxCost就移動左指針 更新結果 Code class Solution { public int equalSubstring(String s, S
2024-07-24
Leetcode > Medium
#Leetcode #心得 #String #Binary Search #Sliding Window #Prefix Sum

1658. Minimum Operations to Reduce X to Zero - Medium

前往題目 想法 Sliding window 思路這題要反著想,不要湊x,要湊sum(nums) - x -> target 雙指針從左邊開始 每個循環更新當前總和 如果比target大,移動左指針 如果找到和target相等的和,紀錄window大小,找最大的window 最後回傳陣列大小減去最大window就是最少操作數 Code class Solution {
2024-07-23
Leetcode > Medium
#Leetcode #心得 #Array #Hash Table #Binary Search #Sliding Window #Prefix Sum

209. Minimum Size Subarray Sum - Medium

前往題目 想法 明顯是sliding window問題 思路 左右指針 從零開始,每次加上當前數字 如果大於或等於target,紀錄最小值,並且移動左指針直到當前總和小於target,才進入下一循環 Code 自己嘗試寫這題時移動左指針的時機沒有寫好,有小bug class Solution { public int minSubArrayLen(int target,
2024-07-22
Leetcode > Medium
#Leetcode #心得 #Array #Binary Search #Sliding Window #Prefix Sum
1…910111213…36

搜尋

Hexo Fluid