個人程式碼練習集中存放區
- 1. Two Sum - go, python
- 20. Valid Parentheses - python
- 26. Remove Duplicates from Sorted Array - go, python
- 27. Remove Element - go, python
- 35. Search Insert position - go
- 53. Maximum Subarray - go, python
- (DP) 70. Climbing Stairs - go
- 88. Merge Sorted Array
- (tree) 94. Binary Tree Inorder Traversal - go (recursive, iterative)
- (tree) 144. Binary Tree Preorder Traversal - go (recursive, iterative)
⚠️ (tree) 145. Binary Tree Postorder Traversal - go... iterative 還想不通 😫- 219. Contains Duplicate II
- 283. Move Zeroes
- 287. Find the Duplicate Number
- 338. Counting Bits
- (DP) 509. Fibonacci Number - go
- 532. K-diff Pairs in an Array
- 561. Array Partition I
- 566. Reshape the Matrix
- 628. Maximum Product of Three Numbers
- 704. BinarySearch
- 746. Min Cost Climbing Stairs👁🗨
- 766. Toeplitz Matrix👁🗨
- 867. Transpose Matrix
- 977. Squares of a Sorted Array👁🗨
- 2. Add Two Numbers
- 3. Longest Substring Without Repeating Characters
- 11. Container With Most Water👁🗨
- 15. 3Sum👁🗨
- 16. 3Sum Closest👁🗨 3-pointers skill
- 19. Remove Nth Node From End of List
- 33. Search in Rotated Sorted Array - python
- 39. Combination Sum 😞👁🗨
- 48. Rotate Image👁🗨
-
- Spiral Matrix
- 56. Merge Intervals
- 75. Sort Colors - go
- 1143. Longest Common Subsequence
- 72. Edit Distance
- related: 1143. Longest Common Subsequence (ref)
- Sortings
- Bloom Filter - a naive proof of concept
- convertIPv4
- christmasTree
- happyNumber: go, ts
- videoPart: go, ts
- factorial_fun: ts
- isBeautifulString: js
- composeRanges
tree
tree pre/in/post-order traversal 的定義: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
1
2 3
4 5
- 所謂的 pre/in/post-order traversal 其實是深度優先(Depth First Traversals)搜尋:
- (a) Inorder (Left, Root, Right) : [4 2 5 1 3]
- (b) Preorder (Root, Left, Right) : [1 2 4 5 3]
- (c) Postorder (Left, Right, Root) : [4 5 2 3 1]
- 而廣度優先(Breadth First or Level Order Traversal)搜尋則會得到: 1 2 3 4 5
https://zh.wikipedia.org/zh-tw/%E9%80%BB%E8%BE%91%E5%BC%82%E6%88%96
基本規則:
p: 0 1 0 1
q: 0 0 1 1
p^q: 0 1 1 0
交換律: p^q = q^p
結合律: (p^q)^r = p^(q^r)
= q^(r^p)
恆等律: p^0 = p
歸零律: p^p = 0
自反: p^q^q = p^0 = p
Brian Kernighan’s Algorithm (count set bits in an integer)
- 用來計算一個整數的二進位表示法裡有多少的
1
- 很神奇地,做一個 while loop,n > 0,並且將 n 與 (n-1) 做幾次 bitwise &,就能知道有幾個
1
cnt = 0
while (n > 0):
n = n & (n-1)
cnt++
發現題目直覺暴力解需要 O(n2),則使用 map
的資料結構歷遍一次(O(n))建立查詢效率 O(1) 的 lookup table,接著再歷遍一次做比較
- 👉 https://leetcode.com/problemset/all/?topicSlugs=array
- 🚫 https://books.halfrost.com/leetcode/ChapterTwo/Array/
references:
目標是要讓 Standard 看得懂最新的語法,否則有一些 parsing error (e.g. when use class private field, we will get Parsing error: Unexpected character '#'
) 導致無法做 auto-fix-on-save。
安裝 babel 相關套件
$ npm install --save-dev @babel/core @babel/cli @babel/preset-env
再於 project root path 放置 babel.config.json
。以下參考自 babel - Usage Guide 的初始設置範例:
⚠️ 內容可能無所謂,只是為了讓 Standard 運作時不要出現找不到 Babel config 的錯誤
{
"presets": [
[
"@babel/env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1"
},
"useBuiltIns": "usage",
"corejs": "3.6.5"
}
]
]
}
安裝 babel-eslint-parser
相關套件
$ npm install eslint @babel/core @babel/eslint-parser --save-dev
再於 project root path 放置 .eslintrc.js
,在此指定要使用何種 parser 來做 linting:
module.exports = {
parser: "@babel/eslint-parser",
};
最後再於 package.js
中設定 standard
使用 @babel/eslint-parser
:
{
...
"standard": {
"parser": "@babel/eslint-parser"
},
...
}
- test/benchmark (https://my.oschina.net/solate/blog/3034188)
go test -bench=. -run=none -benchmem
-bench=.
指的是當前路徑-run=none
run 原本是用來匹配想要執行的單元測試。不去設定會全跑,若想要全部都不跑就指定一個一定不存在的 pattern (none)-benchmem
打開記憶體配置量量測
python createFolder.py -c -t "convertIPv4"
create a codesignal problempython createFolder.py -l -t "283. Move Zeroes"
create a leetcode problemnpm run ts codesignal/happyNumber/happyNumber.ts