Skip to content

Commit f7f7e8d

Browse files
authored
Merge pull request #1456 from jinhyungrhee/week7
[jinhyungrhee] WEEK 07 solutions
2 parents a9f0b04 + 8945532 commit f7f7e8d

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.*;
2+
class Solution {
3+
public int lengthOfLongestSubstring(String s) {
4+
5+
if (s.length() == 0) return 0;
6+
if (s.length() == 1) return 1;
7+
8+
// sliding window
9+
HashSet<Character> table = new HashSet<>();
10+
11+
int left = 0;
12+
int right = 1;
13+
int end = s.length() - 1;
14+
15+
table.add(s.charAt(left));
16+
17+
int maxSize = 0;
18+
while (right <= end) {
19+
20+
if (!table.contains(s.charAt(right))) {
21+
table.add(s.charAt(right));
22+
} else {
23+
/** [์ค‘๋ณต๋œ ๋ฌธ์ž๋ฉด ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ด๋™]
24+
1. ์ค‘๋ณต ๋ฌธ์ž๋ฅผ ๋งŒ๋‚˜๋ฉด left๋ฅผ ํ•œ์นธ์”ฉ ์ฆ๊ฐ€
25+
2. ์ค‘๋ณต ๋ฌธ์ž๊ฐ€ Hash ์—์„œ ์‚ฌ๋ผ์งˆ ๋•Œ๊นŒ์ง€ ์™ผ์ชฝ ๊ฐ’ ์ œ๊ฑฐ
26+
3. ์™ผ์ชฝ ์ค‘๋ณต๋ฌธ์ž๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ์„ ๋•Œ right ์ด๋™
27+
*/
28+
while(table.contains(s.charAt(right))) {
29+
table.remove(s.charAt(left));
30+
left++;
31+
}
32+
table.add(s.charAt(right));
33+
}
34+
35+
int tableSize = table.size();
36+
if (tableSize > maxSize) maxSize = tableSize;
37+
38+
right++;
39+
}
40+
41+
return maxSize;
42+
}
43+
44+
}
45+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
3+
public int numIslands(char[][] grid) {
4+
5+
int[][] dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
6+
boolean[][] visited = new boolean[grid.length][grid[0].length];
7+
8+
int count = 0;
9+
for (int i = 0; i < grid.length; i++) {
10+
for (int j = 0; j < grid[0].length; j++) {
11+
if ('1' == grid[i][j] && !visited[i][j]) {
12+
dfs(grid, visited, dir , i, j);
13+
count++;
14+
}
15+
}
16+
}
17+
return count;
18+
}
19+
20+
public void dfs(char[][] grid, boolean[][] visited, int[][] dir, int x, int y) {
21+
22+
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length
23+
&& !visited[x][y] && '1' == grid[x][y]) {
24+
25+
visited[x][y] = true;
26+
for (int i = 0; i < 4; i++) {
27+
int dx = x + dir[i][0];
28+
int dy = y + dir[i][1];
29+
dfs(grid, visited, dir ,dx, dy);
30+
}
31+
}
32+
}
33+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public ListNode reverseList(ListNode head) {
3+
4+
ListNode prev = null;
5+
ListNode curr = head;
6+
7+
while(curr != null) {
8+
ListNode next = curr.next;
9+
curr.next = prev;
10+
prev = curr;
11+
curr = next;
12+
}
13+
14+
return prev;
15+
}
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
3+
static int MARKER = 987654321;
4+
public void setZeroes(int[][] matrix) {
5+
6+
int width = matrix.length;
7+
int height = matrix[0].length;
8+
9+
for (int i = 0; i < width; i++) {
10+
for (int j = 0; j < height; j++) {
11+
if(matrix[i][j] == 0) {
12+
for (int x = 0; x < width; x++) {
13+
if (matrix[x][j] != 0) matrix[x][j] = MARKER;
14+
}
15+
for (int y = 0; y < height; y++) {
16+
if (matrix[i][y] != 0) matrix[i][y] = MARKER;
17+
}
18+
}
19+
}
20+
}
21+
22+
for (int i = 0; i < width; i++) {
23+
for (int j = 0; j < height; j++) {
24+
if (matrix[i][j] == MARKER) matrix[i][j] = 0;
25+
}
26+
}
27+
28+
}
29+
}

โ€Žunique-paths/jinhyungrhee.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
int count;
3+
public int uniquePaths(int m, int n) {
4+
/**
5+
1. dfs + backtrack => Time Limit Exceeded
6+
*/
7+
//boolean[][] visited = new boolean[m][n];
8+
//dfs(visited, 0, 0, m, n);
9+
10+
/**
11+
2. DP
12+
- ์ €์žฅ๋˜๋Š” ๊ฐ’์€ ํ•ด๋‹น ์œ„์น˜๊นŒ์ง€ ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” '๊ณ ์œ ํ•œ ๊ฒฝ๋กœ์˜ ์ˆ˜'
13+
*/
14+
int[][] dp = new int[m][n];
15+
16+
for (int i = 0; i < m; i++) {
17+
dp[i][0] = 1;
18+
}
19+
for (int j = 0; j < n; j++) {
20+
dp[0][j] = 1;
21+
}
22+
23+
for (int i = 1; i < m; i++) {
24+
for (int j = 1; j < n; j++) {
25+
// ์ด์ „์— ์™”๋˜ ๊ฐ’๋“ค์„ ๋”ํ•จ(์œ„, ์™ผ์ชฝ)
26+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
27+
}
28+
}
29+
30+
return dp[m-1][n-1];
31+
}
32+
33+
/**
34+
1. DFS + backtrack
35+
*/
36+
/**
37+
public void dfs(boolean[][] visited, int x, int y, int m, int n) {
38+
39+
if (x == m - 1 && y == n - 1) {
40+
count++;
41+
return;
42+
}
43+
44+
45+
visited[x][y] = true;
46+
47+
// ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ + backtrack
48+
if (y + 1 < n && !visited[x][y + 1]) {
49+
dfs(visited, x, y + 1, m , n);
50+
}
51+
52+
// ์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™ + backtrack
53+
if (x + 1 < m && !visited[x + 1][y]) {
54+
dfs(visited, x + 1, y, m, n);
55+
}
56+
57+
visited[x][y] = false;
58+
}
59+
*/
60+
}

0 commit comments

Comments
ย (0)