Skip to content

Commit cdd2e31

Browse files
authored
Merge pull request #1480 from lhc0506/main
[lhc0506] Week07 solutions
2 parents 2076a17 + 71cfef1 commit cdd2e31

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
let maxLength = 0;
7+
const charSet = new Set();
8+
9+
let start = 0, end = 0;
10+
11+
while (end < s.length) {
12+
if (charSet.has(s[end])) {
13+
charSet.delete(s[start]);
14+
start += 1;
15+
continue;
16+
}
17+
18+
charSet.add(s[end]);
19+
end += 1;
20+
maxLength = Math.max(maxLength, end - start);
21+
}
22+
23+
return maxLength;
24+
};
25+
26+
// 시간복잡도: O(n)
27+
// 공간복잡도: O(n)

number-of-islands/lhc0506.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
var numIslands = function(grid) {
6+
const m = grid.length;
7+
const n = grid[0].length;
8+
const visited = Array.from(new Array(m), () => new Array(n).fill(false));
9+
const directions = [[0, 1], [0, -1], [1, 0], [-1, 0]];
10+
11+
let islandCount = 0;
12+
13+
for (let i = 0; i < m; i++) {
14+
for (let j = 0; j < n; j++) {
15+
if (grid[i][j] === '1' && !visited[i][j]) {
16+
const queue = [[i, j]];
17+
visited[i][j] = true;
18+
19+
while (queue.length) {
20+
const [y, x] = queue.shift();
21+
22+
for (const [dy, dx] of directions) {
23+
const newY = y + dy;
24+
const newX = x + dx;
25+
26+
if (newY >= 0 && newY < m && newX >= 0 && newX < n && grid[newY][newX] === '1' && !visited[newY][newX]) {
27+
visited[newY][newX] = true;
28+
queue.push([newY, newX]);
29+
}
30+
}
31+
}
32+
33+
islandCount += 1;
34+
}
35+
}
36+
}
37+
38+
return islandCount;
39+
};
40+
41+
// 시간복잡도: O(m * n)
42+
// 공간복잡도: O(m * n)

reverse-linked-list/lhc0506.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function(head) {
13+
let prevNode = null;
14+
let currentNode = head;
15+
16+
while(currentNode) {
17+
const nextNode = currentNode.next;
18+
currentNode.next = prevNode;
19+
20+
prevNode = currentNode;
21+
currentNode = nextNode;
22+
}
23+
24+
return prevNode;
25+
};
26+
27+
// 시간복잡도: O(n)
28+
// 공간복잡도: O(1)

0 commit comments

Comments
 (0)