Skip to content

Commit a9f0b04

Browse files
authored
Merge pull request #1468 from hsskey/main
[hsskey] Week 07 Solutions
2 parents 8c71f77 + 1ff8599 commit a9f0b04

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
const charSet = new Set();
7+
let l = 0;
8+
let res = 0;
9+
10+
for (let r = 0; r < s.length; r++) {
11+
while (charSet.has(s[r])) {
12+
charSet.delete(s[l]);
13+
l += 1;
14+
}
15+
charSet.add(s[r]);
16+
res = Math.max(res, r - l + 1);
17+
}
18+
19+
return res;
20+
};

number-of-islands/hsskey.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
var numIslands = function(grid) {
6+
if (!grid.length) return 0;
7+
8+
const rows = grid.length;
9+
const cols = grid[0].length;
10+
const visit = new Set();
11+
let islands = 0;
12+
13+
const bfs = (r, c) => {
14+
const queue = [];
15+
queue.push([r, c]);
16+
visit.add(`${r},${c}`);
17+
18+
while (queue.length) {
19+
const [row, col] = queue.shift();
20+
const directions = [
21+
[1, 0],
22+
[-1, 0],
23+
[0, 1],
24+
[0, -1],
25+
];
26+
27+
for (const [dr, dc] of directions) {
28+
const newRow = row + dr;
29+
const newCol = col + dc;
30+
31+
if (
32+
newRow >= 0 &&
33+
newRow < rows &&
34+
newCol >= 0 &&
35+
newCol < cols &&
36+
grid[newRow][newCol] === '1' &&
37+
!visit.has(`${newRow},${newCol}`)
38+
) {
39+
queue.push([newRow, newCol]);
40+
visit.add(`${newRow},${newCol}`);
41+
}
42+
}
43+
}
44+
};
45+
46+
for (let r = 0; r < rows; r++) {
47+
for (let c = 0; c < cols; c++) {
48+
if (grid[r][c] === '1' && !visit.has(`${r},${c}`)) {
49+
bfs(r, c);
50+
islands += 1;
51+
}
52+
}
53+
}
54+
55+
return islands;
56+
};

reverse-linked-list/hsskey.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
if (head === null) return null;
14+
15+
let newHead = head;
16+
if (head.next !== null) {
17+
newHead = reverseList(head.next);
18+
19+
head.next.next = head;
20+
}
21+
22+
head.next = null;
23+
24+
return newHead;
25+
};

set-matrix-zeroes/hsskey.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const setZeroes = (matrix) => {
2+
const ROWS = matrix.length;
3+
const COLS = matrix[0].length;
4+
let rowZero = false;
5+
6+
// 1. 어떤 행과 열이 0이 되어야 하는지 기록
7+
for (let r = 0; r < ROWS; r++) {
8+
for (let c = 0; c < COLS; c++) {
9+
if (matrix[r][c] === 0) {
10+
matrix[0][c] = 0;
11+
if (r > 0) {
12+
matrix[r][0] = 0;
13+
} else {
14+
rowZero = true;
15+
}
16+
}
17+
}
18+
}
19+
20+
// 2. 첫 번째 행과 첫 번째 열을 제외한 나머지 처리
21+
for (let r = 1; r < ROWS; r++) {
22+
for (let c = 1; c < COLS; c++) {
23+
if (matrix[0][c] === 0 || matrix[r][0] === 0) {
24+
matrix[r][c] = 0;
25+
}
26+
}
27+
}
28+
29+
// 3. 첫 번째 열 처리
30+
if (matrix[0][0] === 0) {
31+
for (let r = 0; r < ROWS; r++) {
32+
matrix[r][0] = 0;
33+
}
34+
}
35+
36+
// 4. 첫 번째 행 처리
37+
if (rowZero) {
38+
for (let c = 0; c < COLS; c++) {
39+
matrix[0][c] = 0;
40+
}
41+
}
42+
};

unique-paths/hsskey.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} m
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
var uniquePaths = function(m, n) {
7+
let row = new Array(n).fill(1);
8+
9+
for (let i = 0; i < m - 1; i++) {
10+
const newRow = new Array(n).fill(1);
11+
12+
for (let j = n - 2; j >= 0; j--) {
13+
newRow[j] = newRow[j + 1] + row[j];
14+
}
15+
16+
row = newRow;
17+
}
18+
19+
return row[0];
20+
};

0 commit comments

Comments
 (0)