Skip to content

Commit 38fd1ca

Browse files
committed
2020-4-23
1 parent 50c8125 commit 38fd1ca

File tree

10 files changed

+237
-13
lines changed

10 files changed

+237
-13
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package lcci0502_bianry_number_to_string;
2+
3+
/**
4+
* 执行用时:0ms,击败100.00%。消耗内存:36.6MB,击败100.00%。
5+
*/
6+
public class Solution {
7+
public String printBin(double num) {
8+
if (num < 0 || num > 1) {
9+
return "ERROR";
10+
}
11+
if (Double.compare(num, 1) == 0) {
12+
return "1";
13+
}
14+
StringBuilder sb = new StringBuilder("0.");
15+
double tmp = 0.5;
16+
while (true) {
17+
if (num >= tmp) {
18+
sb.append(1);
19+
num -= tmp;
20+
} else {
21+
sb.append(0);
22+
}
23+
tmp /= 2;
24+
if (Double.compare(num, 0) == 0) {
25+
return sb.toString();
26+
}
27+
if (sb.length() > 32) {
28+
return "ERROR";
29+
}
30+
}
31+
}
32+
}

lcci0504_closed_number/Solution.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package lcci0504_closed_number;
2+
3+
/**
4+
* 执行用时:0ms,击败100.00%。消耗内存:36.9MB,击败100.00%。
5+
*/
6+
public class Solution {
7+
public int[] findClosedNumbers(int num) {
8+
return new int[] {getBigger(num), getSmaller(num)};
9+
}
10+
11+
private int getSmaller(int num) {
12+
int index = 0, count1 = 0;
13+
boolean flag = false;
14+
while (index < 32) {
15+
if ((num & (1 << index)) != 0) {
16+
if (flag) {
17+
break;
18+
}
19+
count1++;
20+
} else {
21+
flag = true;
22+
}
23+
index++;
24+
}
25+
num ^= (1 << index);
26+
num |= ((1 << index) - 1);
27+
num &= -(1 << (index - count1 - 1));
28+
if (num <= 0) {
29+
return -1;
30+
}
31+
return num;
32+
}
33+
34+
private int getBigger(int num) {
35+
int index = 0, count0 = 0;
36+
boolean flag = false;
37+
while (index < 32) {
38+
if ((num & (1 << index)) == 0) {
39+
if (flag) {
40+
break;
41+
}
42+
count0++;
43+
} else {
44+
flag = true;
45+
}
46+
index++;
47+
}
48+
num |= (1 << index);
49+
num &= -(1 << index);
50+
num |= ((1 << (index - count0 - 1)) - 1);
51+
if (num <= 0) {
52+
return -1;
53+
}
54+
return num;
55+
}
56+
}

lcci0508_draw_line/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package lcci0508_draw_line;
2+
3+
/**
4+
* 执行用时:0ms,击败100.00%。消耗内存:39.8MB,击败100.00%。
5+
*/
6+
public class Solution {
7+
public int[] drawLine(int length, int w, int x1, int x2, int y) {
8+
int[] result = new int[length];
9+
int index = (w / 32) * y + x1 / 32;
10+
if (length <= index) {
11+
return result;
12+
}
13+
int firstLeft = x1 / 32 * 32, firstRight = firstLeft + 31;
14+
if (x2 <= firstRight) {
15+
int num = 0;
16+
for (int i = x1 - firstLeft; i <= x2 - firstLeft; i++) {
17+
num |= (1 << (31 - i));
18+
}
19+
result[index++] = num;
20+
if (index == result.length) {
21+
return result;
22+
}
23+
} else {
24+
int num = 0;
25+
for (int i = x1 - firstLeft; i < 32; i++) {
26+
num |= (1 << (31 - i));
27+
}
28+
result[index++] = num;
29+
if (index == result.length) {
30+
return result;
31+
}
32+
for (int i = x1 / 32 + 1; i < x2 / 32; i++) {
33+
result[index++] = -1;
34+
if (index == result.length) {
35+
return result;
36+
}
37+
}
38+
num = 0;
39+
for (int i = 0; i <= x2 % 32; i++) {
40+
num |= (1 << (31 - i));
41+
}
42+
result[index++] = num;
43+
if (index == result.length) {
44+
return result;
45+
}
46+
}
47+
return result;
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package lcci0801_three_steps_problem;
2+
3+
/**
4+
* 动态规划。
5+
*
6+
* 时间复杂度是O(n)。空间复杂度是O(1)。
7+
*
8+
* 执行用时:19ms,击败97.88%。消耗内存:36.1MB,击败100.00%。
9+
*/
10+
public class Solution {
11+
public int waysToStep(int n) {
12+
if (n == 1) {
13+
return 1;
14+
} else if (n == 2) {
15+
return 2;
16+
} else if (n == 3) {
17+
return 4;
18+
}
19+
int num1 = 1, num2 = 2, num3 = 4, index = 3, mod = 1000000007;
20+
while (index < n) {
21+
int tmp = num1;
22+
num1 = num2;
23+
num2 = num3;
24+
num3 = ((tmp + num1) % mod + num2) % mod;
25+
index++;
26+
}
27+
return num3;
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package lcci0802_robot_in_a_grid;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 执行用时:1ms,击败100.00%。消耗内存:41.3MB,击败100.00%。
9+
*/
10+
public class Solution {
11+
private int[][] directions = {{1, 0}, {0, 1}};
12+
13+
private int m, n;
14+
15+
public List<List<Integer>> pathWithObstacles(int[][] obstacleGrid) {
16+
m = obstacleGrid.length;
17+
n = obstacleGrid[0].length;
18+
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) {
19+
return new ArrayList<>();
20+
}
21+
return pathWithObstacles(obstacleGrid, new int[]{0, 0});
22+
}
23+
24+
private List<List<Integer>> pathWithObstacles(int[][] obstacleGrid, int[] now) {
25+
obstacleGrid[now[0]][now[1]] = -1;
26+
if (now[0] == m - 1 && now[1] == n - 1) {
27+
List<List<Integer>> result = new ArrayList<>();
28+
result.add(Arrays.asList(now[0], now[1]));
29+
return result;
30+
}
31+
for (int i = 0; i < directions.length; i++) {
32+
int newX = now[0] + directions[i][0], newY = now[1] + directions[i][1];
33+
if (newX >= 0 && newX < m && newY >= 0 && newY < n && obstacleGrid[newX][newY] == 0) {
34+
List<List<Integer>> result = pathWithObstacles(obstacleGrid, new int[] {newX, newY});
35+
if (!result.isEmpty()) {
36+
result.add(0, Arrays.asList(now[0], now[1]));
37+
return result;
38+
}
39+
}
40+
}
41+
return new ArrayList<>();
42+
}
43+
}

lcci0811_coin/Solution.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package lcci0811_coin;
2+
3+
/**
4+
* 执行用时:44ms,击败39.25%。消耗内存:43.7MB,击败100.00%。
5+
*/
6+
public class Solution {
7+
public int waysToChange(int n) {
8+
int[] coins = {25, 10, 5, 1}, dp = new int[n + 1];
9+
dp[0] = 1;
10+
for (int i = 0; i < coins.length; i++) {
11+
for (int j = coins[i]; j <= n; j++) {
12+
dp[j] += dp[j - coins[i]];
13+
dp[j] %= 1000000007;
14+
}
15+
}
16+
return dp[n];
17+
}
18+
}

question0199/Solution1.java renamed to question0199_binary_tree_right_side_view/Solution1.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package question0199;
1+
package question0199_binary_tree_right_side_view;
22

33
import java.util.ArrayList;
44
import java.util.LinkedList;
@@ -37,4 +37,4 @@ public List<Integer> rightSideView(TreeNode root) {
3737
}
3838
return result;
3939
}
40-
}
40+
}

question0199/Solution2.java renamed to question0199_binary_tree_right_side_view/Solution2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package question0199;
1+
package question0199_binary_tree_right_side_view;
22

33
import java.util.ArrayList;
44
import java.util.LinkedList;
@@ -37,4 +37,4 @@ public List<Integer> rightSideView(TreeNode root) {
3737
}
3838
return result;
3939
}
40-
}
40+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package question0199;
1+
package question0199_binary_tree_right_side_view;
22

33
public class TreeNode {
44
int val;
5+
56
TreeNode left;
7+
68
TreeNode right;
79

810
TreeNode(int x) {
911
val = x;
1012
}
11-
}
13+
}

question0518_coin_change_2/Solution2.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package question0518_coin_change_2;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
/**
74
* 对Solution进行降维处理。
85
*
@@ -15,10 +12,8 @@ public int change(int amount, int[] coins) {
1512
int[] dp = new int[amount + 1];
1613
dp[0] = 1;
1714
for (int i = 0; i < coins.length; i++) {
18-
for (int j = 1; j < amount + 1; j++) {
19-
if (j >= coins[i]) {
20-
dp[j] += dp[j - coins[i]];
21-
}
15+
for (int j = coins[i]; j < amount + 1; j++) {
16+
dp[j] += dp[j - coins[i]];
2217
}
2318
}
2419
return dp[amount];

0 commit comments

Comments
 (0)