Skip to content

Commit 61ea403

Browse files
author
rchen102
committed
update
1 parent 68e31cb commit 61ea403

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

Java/leetcode 10.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public boolean isMatch(String s, String p) {
3+
if (s == null || p == null) return s == p;
4+
// dp table
5+
int len1 = s.length(), len2 = p.length();
6+
boolean[][] dp = new boolean[len1+1][len2+1];
7+
// base case
8+
dp[0][0] = true;
9+
for (int j = 0; j < len2; j++) {
10+
if (p.charAt(j) == '*' && dp[0][j-1]) {
11+
dp[0][j+1] = true;
12+
}
13+
}
14+
15+
// dp 推导
16+
for (int i = 1; i <= len1; i++) {
17+
for (int j = 1; j <= len2; j++) {
18+
if (s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '.') {
19+
dp[i][j] = dp[i-1][j-1];
20+
}
21+
if (p.charAt(j-1) == '*') {
22+
// 匹配 0 次
23+
if (s.charAt(i-1) != p.charAt(j-2) && p.charAt(j-2) != '.') {
24+
dp[i][j] = dp[i][j-2];
25+
}
26+
else {
27+
// 匹配 0 或 n 次
28+
dp[i][j] = dp[i-1][j] || dp[i][j-2];
29+
}
30+
}
31+
}
32+
}
33+
return dp[len1][len2];
34+
}
35+
}

Java/leetcode 1293.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Solution: bfs T: O(n) S: O(n)
2+
class Solution {
3+
4+
class Node {
5+
int row;
6+
int col;
7+
int step;
8+
int obs;
9+
10+
public Node(int row, int col, int step, int obs) {
11+
this.row = row;
12+
this.col = col;
13+
this.step = step;
14+
this.obs = obs;
15+
}
16+
}
17+
18+
public int shortestPath(int[][] grid, int k) {
19+
if (grid == null) return -1;
20+
int n = grid.length;
21+
if (grid[0] == null) return -1;
22+
int m = grid[0].length;
23+
24+
Queue<Node> queue = new LinkedList<>();
25+
Node start = new Node(0, 0, 0, 0);
26+
queue.offer(start);
27+
28+
Node[][] visited = new Node[n][m];
29+
visited[0][0] = start;
30+
31+
// 移动方向
32+
int[] dx = new int[]{-1, 1, 0 , 0};
33+
int[] dy = new int[]{0, 0, -1, 1};
34+
35+
while (!queue.isEmpty()) {
36+
int sz = queue.size();
37+
for (int i = 0; i < sz; i++) {
38+
Node tmp = queue.poll();
39+
int row = tmp.row;
40+
int col = tmp.col;
41+
int step = tmp.step;
42+
int obs = tmp.obs;
43+
if (row == n-1 && col == m-1) {
44+
return step;
45+
}
46+
// four direction
47+
for (int j = 0; j < 4; j++) {
48+
int newRow = row + dx[j];
49+
int newCol = col + dy[j];
50+
51+
if (!checkBoundary(newRow, newCol, n, m)) continue;
52+
int newObs = obs + (grid[newRow][newCol] == 1 ? 1 : 0);
53+
if (newObs > k) continue;
54+
Node newNode = new Node(newRow, newCol, step+1, newObs);
55+
// 第一次遍历
56+
if (visited[newRow][newCol] == null) {
57+
visited[newRow][newCol] = newNode;
58+
queue.offer(newNode);
59+
} else {
60+
// 不是第一次遍历该结点,但是新路线障碍物更少
61+
if (newObs < visited[newRow][newCol].obs) {
62+
visited[newRow][newCol] = newNode;
63+
queue.offer(newNode);
64+
}
65+
}
66+
}
67+
}
68+
}
69+
return -1;
70+
71+
}
72+
73+
public static boolean checkBoundary(int row, int col, int n, int m) {
74+
if (row < 0 || row >= n) return false;
75+
if (col < 0 || col >= m) return false;
76+
return true;
77+
}
78+
}

Java/leetcode 191.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
public class Solution {
2+
// you need to treat n as an unsigned value
3+
public int hammingWeight(int n) {
4+
int count = 0;
5+
while (n != 0) {
6+
count++;
7+
n = n & (n-1);
8+
}
9+
return count;
10+
}
11+
}
12+
13+
114
//Solution1: bit manipulation T: O(1) S: O(1)
215
public class Solution {
316
// you need to treat n as an unsigned value

0 commit comments

Comments
 (0)