Skip to content

Commit

Permalink
217th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 11, 2024
1 parent 9710e05 commit cbb322b
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions algorithms/binary-search/README.md
Original file line number Diff line number Diff line change
@@ -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
```
23 changes: 23 additions & 0 deletions algorithms/binary-search/binarySearch.ts
Original file line number Diff line number Diff line change
@@ -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; // 找不到目標
}
9 changes: 8 additions & 1 deletion data-structures/graph/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ class Graph {
// 鄰接表
private adjacencyList: Map<string, string[]>;

constructor() {
constructor(initialAdjList?: { [key: string]: string[] }) {
this.adjacencyList = new Map();

// 初始化
if (initialAdjList) {
for (const vertex in initialAdjList) {
this.adjacencyList.set(vertex, initialAdjList[vertex]);
}
}
}

// 新增頂點
Expand Down
80 changes: 80 additions & 0 deletions data-structures/graph/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
# 圖 (Graph)

## 鄰接表 - 雜湊表方式

```ts
class Graph {
// 鄰接表
private adjacencyList: Map<string, string[]>;

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');
Expand Down Expand Up @@ -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'],
});
```

0 comments on commit cbb322b

Please sign in to comment.