FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

0145_Week07(java) by chinazpj · Pull Request #922 · algorithm007-class01/algorithm007-class01 · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (8) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
55 changes: 55 additions & 0 deletions Week_07/G20200343040145/LeetCode_200_0145.java
This file contains hidden or 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,55 @@
import java.util.Arrays;

public class LeetCode_200_0145 {
public int numIslands(char[][] grid) {
if(grid.length == 0) return 0;
int x = grid.length;
int y = grid[0].length;

int[] nums = new int[x * y];
Arrays.fill(nums, -1);

for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
if(grid[i][j] == '1') {
grid[i][j] = '0';

//判断下侧是否有陆地
if(i < (x - 1) && grid[i + 1][j] == '1') {
union(nums, i * y + j, (i + 1) * y + j);
}

//判断右侧是否有陆地
if(j < (y - 1) && grid[i][j + 1] == '1') {
union(nums, i * y + j, i * y + j + 1);
}
} else {
nums[i * y + j] = -2;
}
}
}

int count = 0;
for(int num : nums) {
if(num == -1) count++;
}

return count;
}

public int find(int[] parents, int i) {
if(parents[i] == -1) {
return i;
}

return find(parents, parents[i]);
}

public void union(int[] parents, int x, int y) {
int xset = find(parents, x);
int yset = find(parents, y);
if(xset != yset) {
parents[xset] = yset;
}
}
}
29 changes: 29 additions & 0 deletions Week_07/G20200343040145/LeetCode_547_0145.java
This file contains hidden or 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 @@
public class LeetCode_547_0145 {

public int findCircleNum(int[][] M) {
int peopleCount = M.length;
//
int[] visited = new int[peopleCount];

int circleCount = 0;
for (int curPeople = 0; curPeople < peopleCount; curPeople++){
// 当前同学未统计过
if (visited[curPeople] == 0){
recursion(M, visited, curPeople, peopleCount);
circleCount++;
}
}

return circleCount;
}

private void recursion(int[][] M, int[] visited, int curPeople, int peopleCount){
for (int recurPeople = 0; recurPeople < peopleCount; recurPeople++){
if (M[curPeople][recurPeople] == 1 && visited[recurPeople] == 0){
visited[recurPeople] = 1;
recursion(M, visited, recurPeople, peopleCount);
}
}
}

}
17 changes: 17 additions & 0 deletions Week_08/G20200343040145/LeetCode_0190_0145.java
This file contains hidden or 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,17 @@
public class LeetCode_0190_0145 {
/**
* 二进制反转
* 10001 => 从右往左 (1 * 2^4) + (0 * 2 ^3) + (0 * 2^2) + (0 * 2^1) + (0 * 2^0)
* */
public int reverseBits(int n) {
int pow = 31;
int res = 0;
while (n != 0 & pow >= 0) {
// 最后一位的值 乘以 幂次
res += (n & 1) << pow;
pow -= 1;
n >>>= 1;
}
return res;
}
}
30 changes: 30 additions & 0 deletions Week_08/G20200343040145/LeetCode_0191_0145.java
This file contains hidden or 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,30 @@
public class LeetCode_0191_0145 {
/**
* 按位与 计算 二进制中 1 的数量
* */
private int hammingWeightTwo(int n) {
int cnt = 0;
while (n != 0) {
cnt += 1;
n = n & (n - 1);
}
return cnt;
}

/**
* 方法① 循环遍历
* */
public int hammingWeightOne(int n) {
// 掩码
int mask = 1;
// 数量
int cnt = 0;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
cnt += 1;
}
mask <<= 1;
}
return cnt;
}
}
11 changes: 11 additions & 0 deletions Week_08/G20200343040145/LeetCode_0231_0145.java
This file contains hidden or 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,11 @@
public class LeetCode_0231_0145 {
/**
* 计算是否时 2 的幂次
* */
public boolean isPowerOfTwo(int n) {
// 1.根据 条件 n >0 排除了 0000000001的情况
// 2.n & (n-1) = 0 代表 二进制中 1 的个数 为 1 个
// 00000000001000 & 00000000000111 = 0 符合条件
return n > 0 && (n & (n - 1)) == 0;
}
}
17 changes: 17 additions & 0 deletions Week_08/G20200343040145/LeetCode_190_0145.java
This file contains hidden or 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,17 @@
public class LeetCode_190_0145 {
/**
* 二进制反转
* 10001 => 从右往左 (1 * 2^4) + (0 * 2 ^3) + (0 * 2^2) + (0 * 2^1) + (0 * 2^0)
* */
public int reverseBits(int n) {
int pow = 31;
int res = 0;
while (n != 0 & pow >= 0) {
// 最后一位的值 乘以 幂次
res += (n & 1) << pow;
pow -= 1;
n >>>= 1;
}
return res;
}
}
30 changes: 30 additions & 0 deletions Week_08/G20200343040145/LeetCode_191_0145.java
This file contains hidden or 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,30 @@
public class LeetCode_191_0145 {
/**
* 按位与 计算 二进制中 1 的数量
* */
private int hammingWeightTwo(int n) {
int cnt = 0;
while (n != 0) {
cnt += 1;
n = n & (n - 1);
}
return cnt;
}

/**
* 方法① 循环遍历
* */
public int hammingWeightOne(int n) {
// 掩码
int mask = 1;
// 数量
int cnt = 0;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
cnt += 1;
}
mask <<= 1;
}
return cnt;
}
}
11 changes: 11 additions & 0 deletions Week_08/G20200343040145/LeetCode_231_0145.java
This file contains hidden or 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,11 @@
public class LeetCode_231_0145 {
/**
* 计算是否时 2 的幂次
* */
public boolean isPowerOfTwo(int n) {
// 1.根据 条件 n >0 排除了 0000000001的情况
// 2.n & (n-1) = 0 代表 二进制中 1 的个数 为 1 个
// 00000000001000 & 00000000000111 = 0 符合条件
return n > 0 && (n & (n - 1)) == 0;
}
}

Back | FazBrowse Home | New Git URL