Skip to content

[minji-go] week 07 solutions #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* <a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/">week07-2.longest-substring-without-repeating-characters</a>
* <li>Description: find the length of the longest substring without duplicate characters</li>
* <li>Topics: Hash Table, String, Sliding Window </li>
* <li>Time Complexity: O(N), Runtime 5ms </li>
* <li>Space Complexity: O(L), Memory 44.66MB </li>
*/

class Solution {
public int lengthOfLongestSubstring(String s) {
int max = 0;
int left = 0;

Map<Character, Integer> chars = new HashMap<>();
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);

if (chars.containsKey(c) && chars.get(c) >= left) {
left = chars.get(c) + 1;
}

chars.put(c, right);
max = Math.max(max, right - left + 1);
}

return max;
}
}
57 changes: 57 additions & 0 deletions number-of-islands/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* <a href="https://leetcode.com/problems/number-of-islands/">week07-3. number-of-islands</a>
* <li>Description: return the number of islands is surrounded by water("0") </li>
* <li>Topics: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix </li>
* <li>Time Complexity: O(M×N), Runtime 5ms </li>
* <li>Space Complexity: O(M×N), Memory 52.11MB </li>
*/
class Solution {
private char[][] grid;
private int m;
private int n;
private boolean[][] visit;
private int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public int numIslands(char[][] grid) {
this.grid = grid;
this.m = grid.length;
this.n = grid[0].length;
this.visit = new boolean[m][n];

int num = 0;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (grid[r][c] == '1' && !visit[r][c]) {
findIsland(r, c);
num++;
}
}
}
return num;
}

public void findIsland(int r, int c) {
Queue<int[]> queue = new LinkedList<>();
visit[r][c] = true;
queue.offer(new int[]{r, c});

while (!queue.isEmpty()) {
int[] cur = queue.poll();
int cr = cur[0];
int cc = cur[1];

for (int[] dir : directions) {
int nr = cr + dir[0];
int nc = cc + dir[1];
if (nr < 0 || nr > m - 1 || nc < 0 || nc > n - 1) {
continue;
}
if (grid[nr][nc] == '1' && !visit[nr][nc]) {
visit[nr][nc] = true;
queue.offer(new int[]{nr, nc});
}
}
}
}

}
21 changes: 21 additions & 0 deletions reverse-linked-list/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* <a href="https://leetcode.com/problems/reverse-linked-list/">week07-1.reverse-linked-list</a>
* <li>Description: return the reversed list </li>
* <li>Topics: Linked List, Recursion </li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(1), Memory 43.04MB </li>
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}

return prev;
}
}