From 3df0940615df1e655995498173ff7f960e351491 Mon Sep 17 00:00:00 2001 From: mike2ox Date: Sat, 25 Jan 2025 14:01:16 +0900 Subject: [PATCH 1/4] feat: Upload reserve-linked-list(typescript) --- reverse-linked-list/mike2ox.ts | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 reverse-linked-list/mike2ox.ts diff --git a/reverse-linked-list/mike2ox.ts b/reverse-linked-list/mike2ox.ts new file mode 100644 index 000000000..dae8b5762 --- /dev/null +++ b/reverse-linked-list/mike2ox.ts @@ -0,0 +1,41 @@ +/** + * https://leetcode.com/problems/reverse-linked-list/ + * 풀이방법: 스택을 사용하여 역순으로 노드를 생성 + * + * 시간복잡도: O(n) (n: 리스트의 길이) + * 공간복잡도: O(n) (스택에 모든 노드를 저장) + */ + +/** + * 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 { + if (!head) return null; + + const stack: number[] = []; + let result: ListNode | null = null; + let lastNode: ListNode; + while (head) { + stack.push(head.val); + head = head.next; + } + for (let i = stack.length - 1; i >= 0; i--) { + if (!result) { + result = new ListNode(stack[i]); + lastNode = result; + continue; + } + lastNode.next = new ListNode(stack[i]); + lastNode = lastNode.next; + } + return result; +} From 1f12369d12ab6d8be539e0dd3ee28e9aebad978d Mon Sep 17 00:00:00 2001 From: mike2ox Date: Sat, 25 Jan 2025 14:22:37 +0900 Subject: [PATCH 2/4] feat: Upload longest-substring-without-repeating-characters(cpp) --- .../mike2ox.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 longest-substring-without-repeating-characters/mike2ox.cpp diff --git a/longest-substring-without-repeating-characters/mike2ox.cpp b/longest-substring-without-repeating-characters/mike2ox.cpp new file mode 100644 index 000000000..ec123ac65 --- /dev/null +++ b/longest-substring-without-repeating-characters/mike2ox.cpp @@ -0,0 +1,27 @@ +/** + * https://leetcode.com/problems/longest-substring-without-repeating-characters/ + * 풀이방법: Sliding Window + * + * 공간 복잡도: O(1) + * 시간 복잡도: O(n) + */ + +#include + +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int answer = 0; + vector visit(256, -1); + int i, j; + + for (i = 0, j = 0; i < s.size(); i++){ + if (visit[s[i]] != -1 && visit[s[i]] >= j && visit[s[i]] < i) { + j = visit[s[i]] + 1; + } + answer = max(answer, i - j + 1); + visit[s[i]] = i; + } + return answer; + } +}; \ No newline at end of file From ef3394ebc7617f1c26bcf67a069db6612b7f8a0f Mon Sep 17 00:00:00 2001 From: mike2ox Date: Sat, 25 Jan 2025 14:26:34 +0900 Subject: [PATCH 3/4] feat: Upload number-of-islands(typescript) --- number-of-islands/mike2ox.ts | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 number-of-islands/mike2ox.ts diff --git a/number-of-islands/mike2ox.ts b/number-of-islands/mike2ox.ts new file mode 100644 index 000000000..2b5335680 --- /dev/null +++ b/number-of-islands/mike2ox.ts @@ -0,0 +1,58 @@ +/** + * https://leetcode.com/problems/number-of-islands/ + * 풀이방법: BFS를 사용하여 섬의 개수를 구함 + * + * 시간복잡도: O(n * m) (n: 그리드의 행, m: 그리드의 열) + * 공간복잡도: O(n * m) (그리드의 모든 요소를 방문) + */ + +function numIslands(grid: string[][]): number { + if (!grid.length) return 0; // 그리드가 비어있는 경우 + + const rows = grid.length; + const cols = grid[0].length; + let islands = 0; + + const bfs = (startRow: number, startCol: number) => { + const q: number[][] = [[startRow, startCol]]; + grid[startRow][startCol] = "0"; + + const directions = [ + [1, 0], + [-1, 0], + [0, 1], + [0, -1], + ]; // 상하좌우 + + while (q.length) { + const [r, c] = q.shift()!; + + for (const [dx, dy] of directions) { + const newR = r + dx; + const newC = c + dy; + + if ( + newR >= 0 && + newR < rows && + newC >= 0 && + newC < cols && + grid[newR][newC] === "1" + ) { + q.push([newR, newC]); + grid[newR][newC] = "0"; + } + } + } + }; + + for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + if (grid[r][c] === "1") { + islands++; + bfs(r, c); + } + } + } + + return islands; +} From dba076e51d305713c251f5b3946be9e28e601ccc Mon Sep 17 00:00:00 2001 From: mike2ox Date: Sat, 25 Jan 2025 14:28:00 +0900 Subject: [PATCH 4/4] fix: Resolve lint error --- longest-substring-without-repeating-characters/mike2ox.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-substring-without-repeating-characters/mike2ox.cpp b/longest-substring-without-repeating-characters/mike2ox.cpp index ec123ac65..3dc0958a1 100644 --- a/longest-substring-without-repeating-characters/mike2ox.cpp +++ b/longest-substring-without-repeating-characters/mike2ox.cpp @@ -24,4 +24,4 @@ class Solution { } return answer; } -}; \ No newline at end of file +};