hjcian / code-practices

code practices (leetcode, codesignal, codewars) by Go, JS, Typescript, Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

個人程式碼練習集中存放區

Problems & practices

Leetcode

Easy

Medium

Hard

classic data structures & algorithms practices

Codewars

CodeSignal

類似觀念集中整理

Binary search variants

基礎知識/DSA

algorithms

data structures

tree

tree pre/in/post-order traversal 的定義: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/

tree

   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

Bitwise operation

XOR 特性

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++

Miscellaneous

發現題目直覺暴力解需要 O(n2),則使用 map 的資料結構歷遍一次(O(n))建立查詢效率 O(1) 的 lookup table,接著再歷遍一次做比較

參考資料

練習順序

Dev environment setup

Enable Standard with babel-eslint/parser

references:

  1. https://babeljs.io/docs/en/usage
  2. https://www.npmjs.com/package/@babel/eslint-parser
  3. https://standardjs.com/#how-do-i-use-experimental-javascript-es-next-features

目標是要讓 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"
  },
  ...
}

Miscellaneous

指令備忘錄

Go

  • test/benchmark (https://my.oschina.net/solate/blog/3034188)
    • go test -bench=. -run=none -benchmem
      • -bench=. 指的是當前路徑
      • -run=none run 原本是用來匹配想要執行的單元測試。不去設定會全跑,若想要全部都不跑就指定一個一定不存在的 pattern (none)
      • -benchmem 打開記憶體配置量量測

folder management

  • python createFolder.py -c -t "convertIPv4" create a codesignal problem
  • python createFolder.py -l -t "283. Move Zeroes" create a leetcode problem
  • npm run ts codesignal/happyNumber/happyNumber.ts

有趣的議題

Custom badge

Partner

About

code practices (leetcode, codesignal, codewars) by Go, JS, Typescript, Python


Languages

Language:Go 65.7%Language:Python 23.1%Language:JavaScript 4.3%Language:Shell 4.1%Language:TypeScript 2.6%Language:Makefile 0.1%