Skip to content

Commit e2635fa

Browse files
authored
Merge pull request #1648 from sukyoungshin/main
2 parents b4308aa + f00cfe4 commit e2635fa

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 1๋ฒˆ ํ’€์ด
2+
function lengthOfLongestSubstring1(s: string): number {
3+
let seen = new Set<string>();
4+
let start = 0;
5+
let maxLength = 0;
6+
7+
for (let end = 0; end < s.length; end++) {
8+
const char = s[end];
9+
10+
// ์ค‘๋ณต๋œ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด Set์—์„œ ์ œ๊ฑฐํ•˜๋ฉด์„œ start๋ฅผ ์•ž์œผ๋กœ ์ด๋™
11+
while (seen.has(char)) {
12+
seen.delete(s[start]);
13+
start++;
14+
}
15+
16+
// ์ค‘๋ณต์ด ์—†์œผ๋ฉด ํ˜„์žฌ ์œˆ๋„์šฐ ๊ธธ์ด ๊ฐฑ์‹ 
17+
seen.add(char);
18+
maxLength = Math.max(maxLength, end - start + 1);
19+
}
20+
21+
return maxLength;
22+
}
23+
24+
// 2๋ฒˆ ํ’€์ด
25+
function lengthOfLongestSubstring2(s: string): number {
26+
let substring = "";
27+
let maxLength = 0;
28+
29+
for (let i = 0; i < s.length; i++) {
30+
const char = s[i];
31+
32+
// ์ค‘๋ณต ๋ฌธ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ทธ ๋ฌธ์ž ์ดํ›„๋ถ€ํ„ฐ ์ž˜๋ผ๋ƒ„
33+
if (substring.includes(char)) {
34+
const index = substring.indexOf(char);
35+
substring = substring.slice(index + 1);
36+
}
37+
38+
substring += char;
39+
maxLength = Math.max(maxLength, substring.length);
40+
}
41+
42+
return maxLength;
43+
}

โ€Žnumber-of-islands/sukyoungshin.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function numIslands(grid: string[][]): number {
2+
let count = 0;
3+
4+
for (let row = 0; row < grid.length; row++) {
5+
for (let col = 0; col < grid[0].length; col++) {
6+
if (grid[row][col] === "1") {
7+
count++; // ์ƒˆ๋กœ์šด ์„ฌ ๋ฐœ๊ฒฌ!
8+
dfs(row, col); // ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  "1"์„ 0์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
9+
}
10+
}
11+
}
12+
13+
function dfs(row: number, col: number) {
14+
// 1. ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์ด๋ฏธ ๋ฌผ(0)์ด๋ฉด return
15+
if (row < 0 || col < 0 || row >= grid.length || col >= grid[0].length)
16+
return;
17+
if (grid[row][col] === "0") return;
18+
19+
// 2. ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ 0์œผ๋กœ ๋ฐ”๊พธ๊ณ 
20+
grid[row][col] = "0";
21+
22+
// 3. ์ƒํ•˜์ขŒ์šฐ๋กœ dfs ์žฌ๊ท€ ํ˜ธ์ถœ
23+
dfs(row - 1, col); // ์œ„
24+
dfs(row + 1, col); // ์•„๋ž˜
25+
dfs(row, col - 1); // ์™ผ์ชฝ
26+
dfs(row, col + 1); // ์˜ค๋ฅธ์ชฝ
27+
}
28+
29+
return count;
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Definition for singly-linked list.
2+
class ListNode {
3+
val: number;
4+
next: ListNode | null;
5+
constructor(val?: number, next?: ListNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.next = next === undefined ? null : next;
8+
}
9+
}
10+
11+
function reverseList(head: ListNode | null): ListNode | null {
12+
let prev: ListNode | null = null;
13+
let current = head;
14+
let next = null;
15+
16+
while (current !== null) {
17+
const next = current.next; // 1. ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ธฐ์–ตํ•ด๋‘๊ณ 
18+
current.next = prev; // 2. ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์ด์ „ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก
19+
prev = current; // 3. ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ง€๊ธˆ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
20+
current = next; // 4. ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
21+
}
22+
23+
return prev;
24+
}

0 commit comments

Comments
ย (0)