1929. Concatenation of Array - Easy 前往題目 想法 串接起來就好 思路 疊代新的陣列(原始的兩倍),並使用index變數來紀錄目前循環到哪個 每次判斷index是否出界了,出界後歸0 否則+1 Codeclass Solution { public int[] getConcatenation(int[] nums) { int[] res = new int[nums.length 2024-03-21 Leetcode > Easy #Leetcode #心得 #Array #Simulation
第一次開發VSCode Extension之流程與心得 前置作業 VSCode Nodejs,可安裝nvm隨意切換與安裝不同版本 Git 初始化專案使用Yeoman來初始化擴展專案 npm install --global yo generator-code yo code Yeoman導覽會帶你一步一步建立專案 設定好一些專案的詳細資訊後就可以開始寫code了! 一些基本文件介紹 F5可以進入debug模式,測試擴展 .vscode資料夾存放程 2024-03-20 Project > VSCode #VSCode Extension #TypeScript #Npm #Git
1461. Check If a String Contains All Binary Codes of Size K - Medium 前往題目 想法 全部比較一遍?這樣需要$O(2^k \times n \times k)$,太沒效率了 思路 疊代整個s,每次切大小k並嘗試加入Set 利用Set的特性,如果可以在s中找到至少$2^k$個unique element,就代表一定是true Code class Solution { public boolean hasAllCodes(String s, 2024-03-20 Leetcode > Medium #Leetcode #心得 #String #Hash Table #Bit Manipulation #Hash Function #Rolling Hash
605. Can Place Flowers - Easy 前往題目 想法 指定一個中點,看左右是否是0 思路 把花床前後各加一個0,這樣就可以在看中點時,不遺漏頭尾的0 疊代整個加長過的花床,遇到一組3個0的就在中間種一朵花 最後如果還有花剩下,false,否則true 自己寫了蠻長的雖然思路對了但是edge case無法解決 Code class Solution { public boolean canPlaceFlower 2024-03-20 Leetcode > Easy #Leetcode #心得 #Array #Greedy
554. Brick Wall - Medium 前往題目 想法 如何找出gap? 思路找出Gap很簡單,直接給他們index代表他們就好 HashMap儲存gap編號以及數量 疊代每道牆,但忽略最後一個磚塊,不然會算到最右邊的cut(穿過0個磚塊) 更新gap的數量以及紀錄最大值 回傳牆的大小減去最大值,就是穿過的磚塊數 Code class Solution { public int leastBricks(Lis 2024-03-19 Leetcode > Medium #Leetcode #心得 #Array #Hash Table
122. Best Time to Buy and Sell Stock II - Medium 前往題目 想法 用DP然後分買與不買 思路可以用複雜的dp但也可以藉由觀察找到最簡單又最優的解 從第二個開始疊代,每次都和前一個比較 如果比上一個大就加到profit裡 因為都在高點賣出,而且可以當天買進,不需要冷卻時間 Code class Solution { public int maxProfit(int[] prices) { in 2024-03-19 Leetcode > Medium #Leetcode #心得 #Array #Greedy #Dynamic Programming
199. Binary Tree Right Side View - Medium 前往題目 之前有寫過 想法 一開始想法非常單純,只看右邊nodes 很快的寫出來發現如果左右不對稱(不等高)的話就行不通 思路 BFS 每層的最右邊就是答案 Code class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> r 2024-03-18 Leetcode > Medium #Leetcode #心得 #Binary Tree #Depth-First Search #Breadth-First Search #Tree
371. Sum of Two Integers - Medium 前往題目 思路 提取數字(用XOR) 提取carry Code class Solution { public int getSum(int a, int b) { // Until no carry while (b != 0) { int temp = (a & b) << 2024-03-18 Leetcode > Medium #Leetcode #心得 #Math #Bit Manipulation
2013. Detect Squares - Medium 前往題目 想法 確定對角線 四邊等長 四個角90° 思路沒想到對角線就可以確定另外兩個點😭 Add 加入列表,並且紀錄個數 Count 疊代列表中所有的點,根據傳入的點算x1 - x2 == y1 - y2,如果是的話就代表這兩點可以繪製出正方形 確認另外兩點是否在列表中 存在就把個數相乘,因為重複的點可再和其他點組成正方形,即便位置一模一樣(題目設定) Code class De 2024-03-15 Leetcode > Medium #Leetcode #心得 #Array #Hash Table #Design #Counting
43. Multiply Strings - Medium 前往題目 思路總之就是模擬乘法過程,不過其中的中繼的數字要放法怎麼放,要加上原本的還是不加… 反轉兩個數字字符串 以直式乘法來看的話,是動下面,再動上面,例如23 * 45,是先3 * 5再3 * 4。和平常反過來 把結果放入陣列,兩個數字的位數加起來就是最多會有幾位 反轉陣列,去掉leading zeros 回傳答案 Code class Solution { publ 2024-03-14 Leetcode > Medium #Leetcode #心得 #String #Math #Simulation