Skip to content

Commit 0d11e57

Browse files
refactor for format
1 parent 170e96d commit 0d11e57

File tree

10 files changed

+116
-44
lines changed

10 files changed

+116
-44
lines changed

src/main/java/com/fishercoder/solutions/_63.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class _63 {
2525
/**Idea: grid[i][j] has to be set to zero if obstacleGrid[i][j] == 1,
2626
* otherwise, we can get dp[i][j] from its top and left dp.*/
2727
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
28-
if(obstacleGrid == null || obstacleGrid.length == 0) return 0;
28+
if(obstacleGrid == null || obstacleGrid.length == 0) {
29+
return 0;
30+
}
2931

3032
int height = obstacleGrid.length, width = obstacleGrid[0].length;
3133
int[][] dp = new int[height][width];
@@ -39,11 +41,16 @@ public int uniquePathsWithObstacles(int[][] obstacleGrid) {
3941

4042
for(int i = 1; i < height; i++){
4143
for(int j = 1; j < width; j++){
42-
if(obstacleGrid[i][j] == 1) dp[i][j] = 0;
43-
else {
44+
if(obstacleGrid[i][j] == 1) {
45+
dp[i][j] = 0;
46+
} else {
4447
int paths = 0;
45-
if(obstacleGrid[i-1][j] == 0) paths += dp[i-1][j];
46-
if(obstacleGrid[i][j-1] == 0) paths += dp[i][j-1];
48+
if(obstacleGrid[i-1][j] == 0) {
49+
paths += dp[i-1][j];
50+
}
51+
if(obstacleGrid[i][j-1] == 0) {
52+
paths += dp[i][j-1];
53+
}
4754
dp[i][j] = paths;
4855
}
4956
}

src/main/java/com/fishercoder/solutions/_631.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ public int sum(int r, char c, String[] strs) {
122122
}
123123

124124
public void topologicalSort(int r, int c) {
125-
for (int i = 0; i < Formulas.length; i++)
126-
for (int j = 0; j < Formulas[0].length; j++)
125+
for (int i = 0; i < Formulas.length; i++) {
126+
for (int j = 0; j < Formulas[0].length; j++) {
127127
if (Formulas[i][j] != null && Formulas[i][j].cells.containsKey("" + (char) ('A' + c) + (r + 1))) {
128128
topologicalSort(i, j);
129129
}
130+
}
131+
}
130132
stack.push(new int[]{r, c});
131133
}
132134

src/main/java/com/fishercoder/solutions/_633.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
*/
1515
public class _633 {
1616
public boolean judgeSquareSum(int c) {
17-
if (c < 0) return false;
17+
if (c < 0) {
18+
return false;
19+
}
1820
int left = 0;
1921
int right = (int) (Math.sqrt(c));
2022
while (left <= right) {

src/main/java/com/fishercoder/solutions/_637.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public class _637 {
3131

3232
public List<Double> averageOfLevels(TreeNode root) {
3333
List<Double> result = new ArrayList<>();
34-
if (root == null) return result;
34+
if (root == null) {
35+
return result;
36+
}
3537
Queue<TreeNode> queue = new LinkedList<>();
3638
queue.offer(root);
3739
while (!queue.isEmpty()) {

src/main/java/com/fishercoder/solutions/_638.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public int shopping(List < Integer > price, List < List < Integer >> special, Li
5454
int j = 0;
5555
for (j = 0; j < special.get(i).size() - 1; j++) {
5656
int diff = clone.get(j) - special.get(i).get(j);
57-
if (diff < 0) break;
57+
if (diff < 0) {
58+
break;
59+
}
5860
clone.set(j, diff);
5961
}
6062
if (j == special.get(i).size() - 1) {

src/main/java/com/fishercoder/solutions/_64.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class _64 {
1212
* have to initialize the first row and the first column and start the for loop from i==1 and j==1 for the rest
1313
* of the matrix.*/
1414
public int minPathSum(int[][] grid) {
15-
if(grid == null || grid.length == 0) return 0;
15+
if(grid == null || grid.length == 0) {
16+
return 0;
17+
}
1618

1719
int height = grid.length, width = grid[0].length;
1820
int[][] dp = new int[height][width];

src/main/java/com/fishercoder/solutions/_640.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,23 @@ public String solveEquation(String equation) {
3939
String[] parts = equation.split("=");
4040
int[] left = evaluate(parts[0]);
4141
int[] right = evaluate(parts[1]);
42-
if (left[0] == right[0] && left[1] == right[1]) return "Infinite solutions";
43-
else if (left[0] == right[0]) return "No solution";
42+
if (left[0] == right[0] && left[1] == right[1]) {
43+
return "Infinite solutions";
44+
} else if (left[0] == right[0]) {
45+
return "No solution";
46+
}
4447
return "x=" + (right[1] - left[1]) / (left[0] - right[0]);
4548
}
4649

4750
private int[] evaluate(String part) {
4851
int[] result = new int[2];//result[0] is the coefficient for x, result[1] is the coefficient for constants
4952
String[] tokens = part.split("(?=[+-])"); // ()for match group; ?= for match and include in res; [+-] means + or -;
5053
for (String token : tokens) {
51-
if (token.equals("+x") || token.equals("x")) result[0]++;
52-
else if (token.equals("-x")) result[0]--;
53-
else if (token.contains("x")) {
54+
if (token.equals("+x") || token.equals("x")) {
55+
result[0]++;
56+
} else if (token.equals("-x")) {
57+
result[0]--;
58+
} else if (token.contains("x")) {
5459
result[0] += Integer.parseInt(token.substring(0, token.length()-1));
5560
} else {
5661
result[1] += Integer.parseInt(token);

src/main/java/com/fishercoder/solutions/_649.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ public String predictPartyVictory(String senate) {
5656
Queue<Integer> direQ = new LinkedList<>();
5757
int len = senate.length();
5858
for (int i = 0; i < len; i++) {
59-
if (senate.charAt(i) == 'R') radiantQ.offer(i);
60-
else direQ.offer(i);
59+
if (senate.charAt(i) == 'R') {
60+
radiantQ.offer(i);
61+
} else {
62+
direQ.offer(i);
63+
}
6164
}
6265
while (!radiantQ.isEmpty() && !direQ.isEmpty()) {
6366
int radiantIndex = radiantQ.poll();

src/main/java/com/fishercoder/solutions/_65.java

+43-16
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,69 @@ public class _65 {
1515
//also, this string could be negative, don't miss this case
1616
public boolean isNumber(String s) {
1717
s = s.trim();
18-
if (s.isEmpty())
18+
if (s.isEmpty()) {
1919
return false;
20-
int eCount = 0, periodCount = 0, index = 0, numberCount = 0;
20+
}
21+
int eCount = 0;
22+
int periodCount = 0;
23+
int index = 0;
24+
int numberCount = 0;
2125
while(index < s.length()) {
22-
if(s.charAt(index) == '.') periodCount++;
23-
if((s.charAt(index) == '-') || s.charAt(index) == '+' || s.charAt(index) == '.') index++;
24-
if(periodCount >= 2) return false;
25-
else break;
26+
if(s.charAt(index) == '.') {
27+
periodCount++;
28+
}
29+
if((s.charAt(index) == '-') || s.charAt(index) == '+' || s.charAt(index) == '.') {
30+
index++;
31+
}
32+
if(periodCount >= 2) {
33+
return false;
34+
} else {
35+
break;
36+
}
37+
}
38+
if(index >= s.length()) {
39+
return false;
2640
}
27-
if(index >= s.length()) return false;
2841
while (index < s.length()) {
2942
if ((Character.getNumericValue(s.charAt(index)) < 10 && Character.getNumericValue(s
3043
.charAt(index)) >= 0)) {
3144
index++;
3245
numberCount++;
3346
continue;
3447
} else if (s.charAt(index) == 'e') {
35-
if(eCount > 1 || numberCount == 0) return false;
48+
if(eCount > 1 || numberCount == 0) {
49+
return false;
50+
}
3651
if (eCount < 2 && index != 0 && index != (s.length() - 1)) {
3752
eCount++;
38-
} else if (index == (s.length() - 1) || index == 0)
53+
} else if (index == (s.length() - 1) || index == 0) {
54+
return false;
55+
}
56+
if(eCount > 1) {
3957
return false;
40-
if(eCount > 1) return false;
58+
}
4159
index++;
4260
//after 'e', there could be '+' or '-' as long as there are numbers after these two signs
4361
if(index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
4462
index++;
45-
if(index >= s.length()) return false;
46-
else continue;
63+
if(index >= s.length()) {
64+
return false;
65+
} else {
66+
continue;
67+
}
4768
}
4869
} else if (s.charAt(index) == '.') {
49-
if(eCount >= 1) return false;
70+
if(eCount >= 1) {
71+
return false;
72+
}
5073
if(index-1 >= 0 && (Character.getNumericValue(s.charAt(index-1)) >= 10 || Character.getNumericValue(s
5174
.charAt(index-1)) < 0)){
5275
if(s.charAt(index-1) == '+' || s.charAt(index-1) == '-') {
5376
index++;
5477
continue;
55-
} else return false;
78+
} else {
79+
return false;
80+
}
5681
}
5782
if(index+1 < s.length() && (Character.getNumericValue(s.charAt(index+1)) >= 10 || Character.getNumericValue(s
5883
.charAt(index+1)) < 0)){
@@ -66,10 +91,12 @@ public boolean isNumber(String s) {
6691
index++;
6792
periodCount++;
6893
}
69-
if (periodCount >= 2 || (index == 0 && index + 1 >= s.length()))
94+
if (periodCount >= 2 || (index == 0 && index + 1 >= s.length())) {
7095
return false;
71-
} else
96+
}
97+
} else {
7298
return false;
99+
}
73100
}
74101
return numberCount != 0;
75102
}

src/main/java/com/fishercoder/solutions/_652.java

+30-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
4242
}
4343

4444
private String postorder(TreeNode curr, HashMap<String, Integer> map, List<TreeNode> res) {
45-
if (curr == null) return "#";
45+
if (curr == null) {
46+
return "#";
47+
}
4648
String serial = curr.val + "," + postorder(curr.left, map, res) + "," + postorder(curr.right, map, res);
4749
if (map.getOrDefault(serial, 0) == 1) {
4850
res.add(curr);
@@ -63,7 +65,9 @@ public class MyOriginalSolution {
6365
*/
6466
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
6567
List<TreeNode> result = new ArrayList<>();
66-
if (root == null) return result;
68+
if (root == null) {
69+
return result;
70+
}
6771
Map<TreeNode, List<TreeNode>> map = new HashMap<>();
6872
Queue<TreeNode> oldQueue = new LinkedList<>();
6973
Queue<TreeNode> newQueue = new LinkedList<>();
@@ -113,21 +117,37 @@ private void findDuplicateSubtrees(TreeNode treeNode, Queue<TreeNode> newQueue,
113117
}
114118

115119
private boolean isSubtree(TreeNode s, TreeNode t) {
116-
if (s == null && t == null) return true;
120+
if (s == null && t == null) {
121+
return true;
122+
}
117123
boolean isSubTree = false;
118-
if (s != null && t != null && s.val == t.val) isSubTree = isSameTree(s, t);
119-
if (isSubTree) return true;
124+
if (s != null && t != null && s.val == t.val) {
125+
isSubTree = isSameTree(s, t);
126+
}
127+
if (isSubTree) {
128+
return true;
129+
}
120130
boolean isSubTreeLeft = false;
121-
if (s.left != null) isSubTreeLeft = isSubtree(s.left, t);
122-
if (isSubTreeLeft) return true;
131+
if (s.left != null) {
132+
isSubTreeLeft = isSubtree(s.left, t);
133+
}
134+
if (isSubTreeLeft) {
135+
return true;
136+
}
123137
boolean isSubTreeRight = false;
124-
if (s.right != null) isSubTreeRight = isSubtree(s.right, t);
125-
if (isSubTreeRight) return true;
138+
if (s.right != null) {
139+
isSubTreeRight = isSubtree(s.right, t);
140+
}
141+
if (isSubTreeRight) {
142+
return true;
143+
}
126144
return false;
127145
}
128146

129147
private boolean isSameTree(TreeNode p, TreeNode q) {
130-
if (p == null || q == null) return p == q;
148+
if (p == null || q == null) {
149+
return p == q;
150+
}
131151
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
132152
}
133153
}

0 commit comments

Comments
 (0)