forked from algorithm004-04/algorithm004-04
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request algorithm004-04#999 from HappyLi000/master
224-Week 01/06/07
- Loading branch information
Showing
6 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); // 右 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |