diff --git a/README.md b/README.md index c62af42..1fdc5ac 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,10 @@ $ pnpm bench twoSum.bench.ts - [圖 (Graph)](./data-structures/graph/README.md) +### Algorithms + +- [二分搜尋 (Binary Search)](./algorithms/binary-search/README.md) + ## Basic - LeetCode 75 Ace Coding Interview with 75 Qs diff --git a/algorithms/binary-search/README.md b/algorithms/binary-search/README.md new file mode 100644 index 0000000..74fc95d --- /dev/null +++ b/algorithms/binary-search/README.md @@ -0,0 +1,33 @@ +# 二分搜尋(Binary Search) + +## 雙閉區間 + +```ts +function binarySearch(arr: number[], target: number): number { + let low = 0; + let high = arr.length - 1; + + while (low <= high) { + const mid = Math.floor((low + high) / 2); + + // 找到目標,返回索引 + if (arr[mid] === target) return mid; + + // 如果 arr[mid] 小於 target,則移動 low 指標 + if (arr[mid] < target) { + low = mid + 1; + } + + // 如果 arr[mid] 大於 target,則移動 high 指標 + if (arr[mid] > target) { + high = mid - 1; + } + } + + return -1; // 找不到目標 +} + +const sortedArray = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46]; +const target = 37; +console.log(binarySearch(sortedArray, target)); // 8 +``` diff --git a/algorithms/binary-search/binarySearch.ts b/algorithms/binary-search/binarySearch.ts new file mode 100644 index 0000000..988c5f1 --- /dev/null +++ b/algorithms/binary-search/binarySearch.ts @@ -0,0 +1,23 @@ +function binarySearch(arr: number[], target: number): number { + let low = 0; + let high = arr.length - 1; + + while (low <= high) { + const mid = Math.floor((low + high) / 2); + + // 找到目標,返回索引 + if (arr[mid] === target) return mid; + + // 如果 arr[mid] 小於 target,則移動 low 指標 + if (arr[mid] < target) { + low = mid + 1; + } + + // 如果 arr[mid] 大於 target,則移動 high 指標 + if (arr[mid] > target) { + high = mid - 1; + } + } + + return -1; // 找不到目標 +} diff --git a/data-structures/graph/Graph.ts b/data-structures/graph/Graph.ts index 3227e26..64efd5e 100644 --- a/data-structures/graph/Graph.ts +++ b/data-structures/graph/Graph.ts @@ -2,8 +2,15 @@ class Graph { // 鄰接表 private adjacencyList: Map; - constructor() { + constructor(initialAdjList?: { [key: string]: string[] }) { this.adjacencyList = new Map(); + + // 初始化 + if (initialAdjList) { + for (const vertex in initialAdjList) { + this.adjacencyList.set(vertex, initialAdjList[vertex]); + } + } } // 新增頂點 diff --git a/data-structures/graph/README.md b/data-structures/graph/README.md index 8740b1e..62001a8 100644 --- a/data-structures/graph/README.md +++ b/data-structures/graph/README.md @@ -1,6 +1,74 @@ # 圖 (Graph) +## 鄰接表 - 雜湊表方式 + ```ts +class Graph { + // 鄰接表 + private adjacencyList: Map; + + constructor(initialAdjList?: { [key: string]: string[] }) { + this.adjacencyList = new Map(); + + // 初始化 + if (initialAdjList) { + for (const vertex in initialAdjList) { + this.adjacencyList.set(vertex, initialAdjList[vertex]); + } + } + } + + // 新增頂點 + addVertex(vertex: string): void { + if (!this.adjacencyList.has(vertex)) { + this.adjacencyList.set(vertex, []); + } + } + + // 移除頂點 + removeVertex(vertex: string): void { + if (this.adjacencyList.has(vertex)) { + for (const adjacentVertex of this.adjacencyList.get(vertex) || []) { + this.removeEdge(vertex, adjacentVertex); + } + + this.adjacencyList.delete(vertex); + } + } + + // 新增邊 + addEdge(vertex1: string, vertex2: string): void { + if (this.adjacencyList.has(vertex1) && this.adjacencyList.has(vertex2)) { + this.adjacencyList.get(vertex1)?.push(vertex2); + this.adjacencyList.get(vertex2)?.push(vertex1); + } + } + + // 移除邊 + removeEdge(vertex1: string, vertex2: string): void { + if (this.adjacencyList.has(vertex1)) { + this.adjacencyList.set( + vertex1, + this.adjacencyList.get(vertex1)?.filter((v) => v !== vertex2) || [], + ); + } + + if (this.adjacencyList.has(vertex2)) { + this.adjacencyList.set( + vertex2, + this.adjacencyList.get(vertex2)?.filter((v) => v !== vertex1) || [], + ); + } + } + + // 印出圖的鄰接表 + print(): void { + for (const [vertex, edges] of this.adjacencyList) { + console.log(`${vertex} -> ${edges.join(', ')}`); + } + } +} + const graph = new Graph(); graph.addVertex('A'); @@ -39,3 +107,15 @@ flowchart LR C o--o D D o--o E ``` + +### 初始化 + +```ts +const graph = new Graph({ + A: ['B', 'C'], + B: ['A', 'D', 'E'], + C: ['A', 'D'], + D: ['B', 'C', 'E'], + E: ['B', 'D'], +}); +```