From c5aadf32f75584df3ad2fcf2c0ea6d668d2fb968 Mon Sep 17 00:00:00 2001 From: Chan Date: Thu, 15 May 2025 16:55:50 +0900 Subject: [PATCH 1/3] reverse-linked-list solution --- reverse-linked-list/lhc0506.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 reverse-linked-list/lhc0506.js diff --git a/reverse-linked-list/lhc0506.js b/reverse-linked-list/lhc0506.js new file mode 100644 index 000000000..f4577b116 --- /dev/null +++ b/reverse-linked-list/lhc0506.js @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + let prevNode = null; + let currentNode = head; + + while(currentNode) { + const nextNode = currentNode.next; + currentNode.next = prevNode; + + prevNode = currentNode; + currentNode = nextNode; + } + + return prevNode; +}; + +// 시간복잡도: O(n) +// 공간복잡도: O(1) From 22ae50f91610a91990b2e44e4fa368fc0e2ad0c2 Mon Sep 17 00:00:00 2001 From: Chan Date: Fri, 16 May 2025 17:08:57 +0900 Subject: [PATCH 2/3] number-of-islands solution --- number-of-islands/lhc0506.js | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 number-of-islands/lhc0506.js diff --git a/number-of-islands/lhc0506.js b/number-of-islands/lhc0506.js new file mode 100644 index 000000000..5e5647832 --- /dev/null +++ b/number-of-islands/lhc0506.js @@ -0,0 +1,42 @@ +/** + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function(grid) { + const m = grid.length; + const n = grid[0].length; + const visited = Array.from(new Array(m), () => new Array(n).fill(false)); + const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]; + + let islandCount = 0; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] === '1' && !visited[i][j]) { + const queue = [[i, j]]; + visited[i][j] = true; + + while (queue.length) { + const [y, x] = queue.shift(); + + for (const [dy, dx] of directions) { + const newY = y + dy; + const newX = x + dx; + + if (newY >= 0 && newY < m && newX >= 0 && newX < n && grid[newY][newX] === '1' && !visited[newY][newX]) { + visited[newY][newX] = true; + queue.push([newY, newX]); + } + } + } + + islandCount += 1; + } + } + } + + return islandCount; +}; + +// 시간복잡도: O(m * n) +// 공간복잡도: O(m * n) From 71cfef137c3607b863557b76a6de5fbfac31d97b Mon Sep 17 00:00:00 2001 From: Chan Date: Fri, 16 May 2025 17:09:32 +0900 Subject: [PATCH 3/3] longest-substring-without-repeating-characters solution --- .../lhc0506.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 longest-substring-without-repeating-characters/lhc0506.js diff --git a/longest-substring-without-repeating-characters/lhc0506.js b/longest-substring-without-repeating-characters/lhc0506.js new file mode 100644 index 000000000..9e28d120e --- /dev/null +++ b/longest-substring-without-repeating-characters/lhc0506.js @@ -0,0 +1,27 @@ +/** + * @param {string} s + * @return {number} + */ +var lengthOfLongestSubstring = function(s) { + let maxLength = 0; + const charSet = new Set(); + + let start = 0, end = 0; + + while (end < s.length) { + if (charSet.has(s[end])) { + charSet.delete(s[start]); + start += 1; + continue; + } + + charSet.add(s[end]); + end += 1; + maxLength = Math.max(maxLength, end - start); + } + + return maxLength; +}; + +// 시간복잡도: O(n) +// 공간복잡도: O(n)