Skip to content

Commit

Permalink
Merge pull request algorithm004-04#999 from HappyLi000/master
Browse files Browse the repository at this point in the history
224-Week 01/06/07
  • Loading branch information
Daisy3485 authored Dec 3, 2019
2 parents 49155a4 + a7f1765 commit 6283957
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Week 01/id_224/LeetCode_21_224.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);

ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}

prev.next = l1 == null ? l2 : l1;

return prehead.next;
}
}
13 changes: 13 additions & 0 deletions Week 01/id_224/LeetCode_26_224.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
38 changes: 38 additions & 0 deletions Week 06/id_224/LeetCode_130_224.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0) return;
int m = board.length;
int n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// 从边缘o开始搜索
boolean isEdge = i == 0 || j == 0 || i == m - 1 || j == n - 1;
if (isEdge && board[i][j] == 'O') {
dfs(board, i, j);
}
}
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
}
if (board[i][j] == '#') {
board[i][j] = 'O';
}
}
}
}

public void dfs(char[][] board, int i, int j) {
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] == 'X' || board[i][j] == '#') {
return;
}
board[i][j] = '#';
dfs(board, i - 1, j); // 上
dfs(board, i + 1, j); // 下
dfs(board, i, j - 1); // 左
dfs(board, i, j + 1); // 右
}
}
80 changes: 80 additions & 0 deletions Week 06/id_224/LeetCode_200_224.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class Solution {
class UnionFind {
int count;
int[] parent;
int[] rank;

public UnionFind(char[][] grid) {
count = 0;
int m = grid.length;
int n = grid[0].length;
parent = new int[m * n];
rank = new int[m * n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
parent[i * n + j] = i * n + j;
++count;
}
rank[i * n + j] = 0;
}
}
}

public int find(int i) { // path compression
if (parent[i] != i) parent[i] = find(parent[i]);
return parent[i];
}

public void union(int x, int y) { // union with rank
int rootx = find(x);
int rooty = find(y);
if (rootx != rooty) {
if (rank[rootx] > rank[rooty]) {
parent[rooty] = rootx;
} else if (rank[rootx] < rank[rooty]) {
parent[rootx] = rooty;
} else {
parent[rooty] = rootx; rank[rootx] += 1;
}
--count;
}
}

public int getCount() {
return count;
}
}

public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}

int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
UnionFind uf = new UnionFind(grid);
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
grid[r][c] = '0';
if (r - 1 >= 0 && grid[r-1][c] == '1') {
uf.union(r * nc + c, (r-1) * nc + c);
}
if (r + 1 < nr && grid[r+1][c] == '1') {
uf.union(r * nc + c, (r+1) * nc + c);
}
if (c - 1 >= 0 && grid[r][c-1] == '1') {
uf.union(r * nc + c, r * nc + c - 1);
}
if (c + 1 < nc && grid[r][c+1] == '1') {
uf.union(r * nc + c, r * nc + c + 1);
}
}
}
}

return uf.getCount();
}
}
27 changes: 27 additions & 0 deletions Week 07/id_224/LeetCode_1122_224.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] m = new int[1001];

int[] ref = new int[arr1.length];

for(int i = 0; i < arr1.length; i++) {
m[arr1[i]]++;
}

int cnt = 0;
for(int i = 0; i < arr2.length; i++) {
while(m[arr2[i]] > 0) {
ref[cnt++] = arr2[i];
m[arr2[i]]--;
}
}

for(int i = 0; i < 1001; i++) {
while(m[i] > 0) {
ref[cnt++] = i;
m[i]--;
}
}
return ref;
}
}
12 changes: 12 additions & 0 deletions Week 07/id_224/LeetCode_242_224.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1, str2);
}
}

0 comments on commit 6283957

Please sign in to comment.