From 2839bd9efd23ca2063fccc9c8135bee65de5d2ff Mon Sep 17 00:00:00 2001 From: suKyoung Date: Fri, 4 Jul 2025 22:00:18 +0900 Subject: [PATCH 1/4] #223 reverse linked list --- reverse-linked-list/sukyoungshin.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 reverse-linked-list/sukyoungshin.ts diff --git a/reverse-linked-list/sukyoungshin.ts b/reverse-linked-list/sukyoungshin.ts new file mode 100644 index 000000000..4b8357c5e --- /dev/null +++ b/reverse-linked-list/sukyoungshin.ts @@ -0,0 +1,25 @@ +// Definition for singly-linked list. +class ListNode { + val: number + next: ListNode | null + constructor(val?: number, next?: ListNode | null) { + this.val = (val===undefined ? 0 : val) + this.next = (next===undefined ? null : next) + } +} + +function reverseList(head: ListNode | null): ListNode | null { + let prev: ListNode | null = null; + let current = head; + let next = null; + + while (current !== null) { + const next = current.next; // 1. 다음 노드를 기억해두고 + current.next = prev; // 2. 현재 노드가 이전 노드를 가리키도록 + prev = current; // 3. 이전 노드를 지금 노드로 업데이트 + current = next; // 4. 현재 노드를 다음 노드로 이동 + } + + + return prev; +}; \ No newline at end of file From 20c992949a32d76f8c74968d88e59252aab26d38 Mon Sep 17 00:00:00 2001 From: suKyoung Date: Sat, 5 Jul 2025 16:15:11 +0900 Subject: [PATCH 2/4] #243 Longest Substring Without Repeating Characters --- .../sukyoungshin.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 longest-substring-without-repeating-characters/sukyoungshin.ts diff --git a/longest-substring-without-repeating-characters/sukyoungshin.ts b/longest-substring-without-repeating-characters/sukyoungshin.ts new file mode 100644 index 000000000..89497c889 --- /dev/null +++ b/longest-substring-without-repeating-characters/sukyoungshin.ts @@ -0,0 +1,43 @@ +// 1번 풀이 +function lengthOfLongestSubstring1(s: string): number { + let seen = new Set(); + let start = 0; + let maxLength = 0; + + for (let end = 0; end < s.length; end++) { + const char = s[end]; + + // 중복된 문자가 나오면 Set에서 제거하면서 start를 앞으로 이동 + while (seen.has(char)) { + seen.delete(s[start]); + start++; + } + + // 중복이 없으면 현재 윈도우 길이 갱신 + seen.add(char); + maxLength = Math.max(maxLength, end - start + 1); + } + + return maxLength; +} + +// 2번 풀이 +function lengthOfLongestSubstring2(s: string): number { + let substring = ""; + let maxLength = 0; + + for (let i = 0; i < s.length; i++) { + const char = s[i]; + + // 중복 문자가 있다면, 그 문자 이후부터 잘라냄 + if (substring.includes(char)) { + const index = substring.indexOf(char); + substring = substring.slice(index + 1); + } + + substring += char; + maxLength = Math.max(maxLength, substring.length); + } + + return maxLength; +} From 5c6475510283cdc919735fb77b0b5fd628a41f44 Mon Sep 17 00:00:00 2001 From: suKyoung Date: Sun, 6 Jul 2025 12:04:29 +0900 Subject: [PATCH 3/4] #258 Number of Islands --- number-of-islands/sukyoungshin.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 number-of-islands/sukyoungshin.ts diff --git a/number-of-islands/sukyoungshin.ts b/number-of-islands/sukyoungshin.ts new file mode 100644 index 000000000..8d0f8b447 --- /dev/null +++ b/number-of-islands/sukyoungshin.ts @@ -0,0 +1,30 @@ +function numIslands(grid: string[][]): number { + let count = 0; + + for (let row = 0; row < grid.length; row++) { + for (let col = 0; col < grid[0].length; col++) { + if (grid[row][col] === "1") { + count++; // 새로운 섬 발견! + dfs(row, col); // 연결된 모든 "1"을 0으로 바꾸기 + } + } + } + + function dfs(row: number, col: number) { + // 1. 범위를 벗어나거나 이미 물(0)이면 return + if (row < 0 || col < 0 || row >= grid.length || col >= grid[0].length) + return; + if (grid[row][col] === "0") return; + + // 2. 현재 좌표를 0으로 바꾸고 + grid[row][col] = "0"; + + // 3. 상하좌우로 dfs 재귀 호출 + dfs(row - 1, col); // 위 + dfs(row + 1, col); // 아래 + dfs(row, col - 1); // 왼쪽 + dfs(row, col + 1); // 오른쪽 + } + + return count; +} From 609212c7740cd18c2bf9b0e18840bdf29af4ab9c Mon Sep 17 00:00:00 2001 From: suKyoung Date: Sun, 6 Jul 2025 12:07:58 +0900 Subject: [PATCH 4/4] fix: lint error --- reverse-linked-list/sukyoungshin.ts | 35 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/reverse-linked-list/sukyoungshin.ts b/reverse-linked-list/sukyoungshin.ts index 4b8357c5e..7475ef11c 100644 --- a/reverse-linked-list/sukyoungshin.ts +++ b/reverse-linked-list/sukyoungshin.ts @@ -1,25 +1,24 @@ // Definition for singly-linked list. class ListNode { - val: number - next: ListNode | null - constructor(val?: number, next?: ListNode | null) { - this.val = (val===undefined ? 0 : val) - this.next = (next===undefined ? null : next) - } + val: number; + next: ListNode | null; + constructor(val?: number, next?: ListNode | null) { + this.val = val === undefined ? 0 : val; + this.next = next === undefined ? null : next; + } } function reverseList(head: ListNode | null): ListNode | null { - let prev: ListNode | null = null; - let current = head; - let next = null; + let prev: ListNode | null = null; + let current = head; + let next = null; - while (current !== null) { - const next = current.next; // 1. 다음 노드를 기억해두고 - current.next = prev; // 2. 현재 노드가 이전 노드를 가리키도록 - prev = current; // 3. 이전 노드를 지금 노드로 업데이트 - current = next; // 4. 현재 노드를 다음 노드로 이동 - } + while (current !== null) { + const next = current.next; // 1. 다음 노드를 기억해두고 + current.next = prev; // 2. 현재 노드가 이전 노드를 가리키도록 + prev = current; // 3. 이전 노드를 지금 노드로 업데이트 + current = next; // 4. 현재 노드를 다음 노드로 이동 + } - - return prev; -}; \ No newline at end of file + return prev; +}