diff --git "a/LeetCode\351\241\214\347\233\256\347\255\206\350\250\230.md" "b/LeetCode\351\241\214\347\233\256\347\255\206\350\250\230.md" new file mode 100644 index 0000000000..5b96faef52 --- /dev/null +++ "b/LeetCode\351\241\214\347\233\256\347\255\206\350\250\230.md" @@ -0,0 +1,92 @@ +# LeetCode 題目筆記 + +## Stack +### 856. Score of Parentheses +題目 +給定一個平衡的括號字串 `s`,返回字串的分數。 +1. `"()"` 的分數為 1。 +2. `AB` 的分數為 `A + B`,其中 `A` 和 `B` 是平衡的括號字串。 +3. `"(A)"` 的分數為 `2 * A`,其中 `A` 是平衡的括號字串。 +解法 +輸入:"(()(()))" +- 依序將'('放入stack +- 若遇到')'則開始pop +- 紀錄pop出來的數字直到pop出的是'(' +- 將pop出來的數字相加放入stack,若沒有數字則push(1) +- 最後將stack內的數字加總 +*** +### 1209. Remove All Adjacent Duplicates in String II +題目 +給定字串 s 和整數 k,每次可以移除 s 中相鄰且相同的 k 個字元,移除後會將左右兩邊的字串連接起來。 +重複上述操作直到無法再進行移除,最後返回處理後的字串。答案保證是唯一的。 +解法 +* 先把字串內字元放入堆疊st1 +* st1字元依序pop到st2並記錄次數 +* 若連續出現k次則把st2的字元pop出k次,刪除重複字元 +* st2再pop k-1次並push到st1內,避免後續會有k個連續無法被檢查到 +* 持續上述步驟直到st1為空 +* 將st2的字元組合成字串並反轉即為答案 +*** +## String + +## 408. Valid Word Abbreviation +題目 +字串可被替換為數字與字母的組合 +例如:"s10n" ("s ubstitutio n") +檢查替換是否符合規則 +Example 1: +Input: word = "internationalization", abbr = "i12iz4n" +Output: true + +解法 +* 依序檢查abbr,若為字母則檢查和word首字是否相等,若相等則刪除word[0] +* 若為數字則先放入暫存,等又是字母時將word刪除暫存數量的字母數 +* 最後若word為空則表示true +*** + +### 553. Optimal Division +題目 +給你一個整數陣列nums = [2,3,4],我們將計算表達式"2/3/4"。 +可在任何位置新增任意數量的括號來變更運算的優先權。求表達式的值達到最大值。 +輸入: nums = [1000,100,10,2] +輸出: "1000/(100/10/2)" +解法 +* 分母最小則運算值最大 +* 因此答案固定將括號放在分子的部分 +*** + +### 893. Groups of Special-Equivalent Strings +題目 +可以交換字串中任意兩個偶數索引字元或任意兩個奇數索引字元。 +如果經過任意次數的移動讓words[i] == words[j],則words[i]和words[j]是特殊等價的兩個字串。 +傳回特殊等效字串組的數量 +輸入:words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"] +輸出:3 +解法 +* 取奇數、偶數索引的字元各自排列 +* 組合起來取代源字元 +* 用set()看不同的字串有幾種 +*** +### 791. Custom Sort String +題目 +給兩個字串order和s,s需照order的順序排,沒在order內的字元則可以出現在任意位置 +Example 1: +Input: order = "cba", s = "abcd" +Output: "cbad" +解法 +* unordered_map紀錄s中有出現在order的次數。vector存沒在order的字元 +* 依照unordered_map紀錄的結果來生成輸出字串 +* 將沒出現的依序放到後面 +*** + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index 09c54afa69..0000000000 --- a/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# What is this? - -The github.dev web-based editor is a lightweight editing experience that runs entirely in your browser. You can navigate files and source code repositories from GitHub, and make and commit code changes. - -There are two ways to go directly to a VS Code environment in your browser and start coding: - -* Press the . key on any repository or pull request. -* Swap `.com` with `.dev` in the URL. For example, this repo https://github.com/github/dev becomes http://github.dev/github/dev - -Preview the gif below to get a quick demo of github.dev in action. - -![github dev](https://user-images.githubusercontent.com/856858/130119109-4769f2d7-9027-4bc4-a38c-10f297499e8f.gif) - -# Why? -It’s a quick way to edit and navigate code. It's especially useful if you want to edit multiple files at a time or take advantage of all the powerful code editing features of Visual Studio Code when making a quick change. For more information, see our [documentation](https://github.co/codespaces-editor-help).