Skip to content

Commit 831489e

Browse files
authored
Merge pull request #1474 from minji-go/week07
[minji-go] week 07 solutions
2 parents a0e0700 + 664e024 commit 831489e

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">week07-2.longest-substring-without-repeating-characters</a>
3+
* <li>Description: find the length of the longest substring without duplicate characters</li>
4+
* <li>Topics: Hash Table, String, Sliding Window </li>
5+
* <li>Time Complexity: O(N), Runtime 5ms </li>
6+
* <li>Space Complexity: O(L), Memory 44.66MB </li>
7+
*/
8+
9+
class Solution {
10+
public int lengthOfLongestSubstring(String s) {
11+
int max = 0;
12+
int left = 0;
13+
14+
Map<Character, Integer> chars = new HashMap<>();
15+
for (int right = 0; right < s.length(); right++) {
16+
char c = s.charAt(right);
17+
18+
if (chars.containsKey(c) && chars.get(c) >= left) {
19+
left = chars.get(c) + 1;
20+
}
21+
22+
chars.put(c, right);
23+
max = Math.max(max, right - left + 1);
24+
}
25+
26+
return max;
27+
}
28+
}

number-of-islands/minji-go.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/number-of-islands/">week07-3. number-of-islands</a>
3+
* <li>Description: return the number of islands is surrounded by water("0") </li>
4+
* <li>Topics: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix </li>
5+
* <li>Time Complexity: O(M×N), Runtime 5ms </li>
6+
* <li>Space Complexity: O(M×N), Memory 52.11MB </li>
7+
*/
8+
class Solution {
9+
private char[][] grid;
10+
private int m;
11+
private int n;
12+
private boolean[][] visit;
13+
private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
14+
15+
public int numIslands(char[][] grid) {
16+
this.grid = grid;
17+
this.m = grid.length;
18+
this.n = grid[0].length;
19+
this.visit = new boolean[m][n];
20+
21+
int num = 0;
22+
for (int r = 0; r < m; r++) {
23+
for (int c = 0; c < n; c++) {
24+
if (grid[r][c] == '1' && !visit[r][c]) {
25+
findIsland(r, c);
26+
num++;
27+
}
28+
}
29+
}
30+
return num;
31+
}
32+
33+
public void findIsland(int r, int c) {
34+
Queue<int[]> queue = new LinkedList<>();
35+
visit[r][c] = true;
36+
queue.offer(new int[]{r, c});
37+
38+
while (!queue.isEmpty()) {
39+
int[] cur = queue.poll();
40+
int cr = cur[0];
41+
int cc = cur[1];
42+
43+
for (int[] dir : directions) {
44+
int nr = cr + dir[0];
45+
int nc = cc + dir[1];
46+
if (nr < 0 || nr > m - 1 || nc < 0 || nc > n - 1) {
47+
continue;
48+
}
49+
if (grid[nr][nc] == '1' && !visit[nr][nc]) {
50+
visit[nr][nc] = true;
51+
queue.offer(new int[]{nr, nc});
52+
}
53+
}
54+
}
55+
}
56+
57+
}

reverse-linked-list/minji-go.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/reverse-linked-list/">week07-1.reverse-linked-list</a>
3+
* <li>Description: return the reversed list </li>
4+
* <li>Topics: Linked List, Recursion </li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 43.04MB </li>
7+
*/
8+
class Solution {
9+
public ListNode reverseList(ListNode head) {
10+
ListNode prev = null;
11+
ListNode curr = head;
12+
while (curr != null) {
13+
ListNode temp = curr.next;
14+
curr.next = prev;
15+
prev = curr;
16+
curr = temp;
17+
}
18+
19+
return prev;
20+
}
21+
}

unique-paths/minji-go.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/unique-paths/">week07-4. unique-paths</a>
3+
* <li>Description: return the number of possible unique paths to reach the bottom-right corner</li>
4+
* <li>Topics: Math, Dynamic Programming, Combinatorics </li>
5+
* <li>Time Complexity: O(M×N), Runtime 2ms </li>
6+
* <li>Space Complexity: O(M×N), Memory 40.76MB </li>
7+
*/
8+
class Solution {
9+
public int uniquePaths(int m, int n) {
10+
int[][] dirs = {{1, 0}, {0, 1}};
11+
int[][] dp = new int[m][n];
12+
dp[0][0] = 1;
13+
14+
Queue<int[]> queue = new LinkedList<>();
15+
queue.add(new int[]{0, 0});
16+
17+
while (!queue.isEmpty()) {
18+
int[] cur = queue.poll();
19+
int cr = cur[0];
20+
int cc = cur[1];
21+
22+
for (int i = 0; i < 2; i++) {
23+
int nr = cr + dirs[i][0];
24+
int nc = cc + dirs[i][1];
25+
26+
if (nr > m - 1 || nc > n - 1) {
27+
continue;
28+
}
29+
30+
if (dp[nr][nc] == 0) {
31+
queue.add(new int[]{nr, nc});
32+
}
33+
dp[nr][nc] += dp[cr][cc];
34+
}
35+
}
36+
37+
return dp[m - 1][n - 1];
38+
}
39+
}

0 commit comments

Comments
 (0)