Skip to content

Commit 4e76819

Browse files
authored
Merge pull request #949 from mike2ox/main
[moonhyeok] Week7
2 parents cce4f9e + dba076e commit 4e76819

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
3+
* 풀이방법: Sliding Window
4+
*
5+
* 공간 복잡도: O(1)
6+
* 시간 복잡도: O(n)
7+
*/
8+
9+
#include <vector>
10+
11+
class Solution {
12+
public:
13+
int lengthOfLongestSubstring(string s) {
14+
int answer = 0;
15+
vector<int> visit(256, -1);
16+
int i, j;
17+
18+
for (i = 0, j = 0; i < s.size(); i++){
19+
if (visit[s[i]] != -1 && visit[s[i]] >= j && visit[s[i]] < i) {
20+
j = visit[s[i]] + 1;
21+
}
22+
answer = max(answer, i - j + 1);
23+
visit[s[i]] = i;
24+
}
25+
return answer;
26+
}
27+
};

number-of-islands/mike2ox.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* https://leetcode.com/problems/number-of-islands/
3+
* 풀이방법: BFS를 사용하여 섬의 개수를 구함
4+
*
5+
* 시간복잡도: O(n * m) (n: 그리드의 행, m: 그리드의 열)
6+
* 공간복잡도: O(n * m) (그리드의 모든 요소를 방문)
7+
*/
8+
9+
function numIslands(grid: string[][]): number {
10+
if (!grid.length) return 0; // 그리드가 비어있는 경우
11+
12+
const rows = grid.length;
13+
const cols = grid[0].length;
14+
let islands = 0;
15+
16+
const bfs = (startRow: number, startCol: number) => {
17+
const q: number[][] = [[startRow, startCol]];
18+
grid[startRow][startCol] = "0";
19+
20+
const directions = [
21+
[1, 0],
22+
[-1, 0],
23+
[0, 1],
24+
[0, -1],
25+
]; // 상하좌우
26+
27+
while (q.length) {
28+
const [r, c] = q.shift()!;
29+
30+
for (const [dx, dy] of directions) {
31+
const newR = r + dx;
32+
const newC = c + dy;
33+
34+
if (
35+
newR >= 0 &&
36+
newR < rows &&
37+
newC >= 0 &&
38+
newC < cols &&
39+
grid[newR][newC] === "1"
40+
) {
41+
q.push([newR, newC]);
42+
grid[newR][newC] = "0";
43+
}
44+
}
45+
}
46+
};
47+
48+
for (let r = 0; r < rows; r++) {
49+
for (let c = 0; c < cols; c++) {
50+
if (grid[r][c] === "1") {
51+
islands++;
52+
bfs(r, c);
53+
}
54+
}
55+
}
56+
57+
return islands;
58+
}

reverse-linked-list/mike2ox.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* https://leetcode.com/problems/reverse-linked-list/
3+
* 풀이방법: 스택을 사용하여 역순으로 노드를 생성
4+
*
5+
* 시간복잡도: O(n) (n: 리스트의 길이)
6+
* 공간복잡도: O(n) (스택에 모든 노드를 저장)
7+
*/
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* class ListNode {
12+
* val: number
13+
* next: ListNode | null
14+
* constructor(val?: number, next?: ListNode | null) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.next = (next===undefined ? null : next)
17+
* }
18+
* }
19+
*/
20+
21+
function reverseList(head: ListNode | null): ListNode | null {
22+
if (!head) return null;
23+
24+
const stack: number[] = [];
25+
let result: ListNode | null = null;
26+
let lastNode: ListNode;
27+
while (head) {
28+
stack.push(head.val);
29+
head = head.next;
30+
}
31+
for (let i = stack.length - 1; i >= 0; i--) {
32+
if (!result) {
33+
result = new ListNode(stack[i]);
34+
lastNode = result;
35+
continue;
36+
}
37+
lastNode.next = new ListNode(stack[i]);
38+
lastNode = lastNode.next;
39+
}
40+
return result;
41+
}

0 commit comments

Comments
 (0)