Skip to content

Commit 981ef79

Browse files
refactor for format
1 parent e2eec22 commit 981ef79

25 files changed

+167
-61
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ public int findLonelyPixel(char[][] picture) {
3737
private boolean isLonely(int row, int col, char[][] picture, int m, int n) {
3838
for (int i = 0; i < m; i++) {
3939
if (i != row) {
40-
if (picture[i][col] == 'B') return false;
40+
if (picture[i][col] == 'B') {
41+
return false;
42+
}
4143
}
4244
}
4345

4446
for (int j = 0; j < n; j++) {
4547
if (j != col) {
46-
if (picture[row][j] == 'B') return false;
48+
if (picture[row][j] == 'B') {
49+
return false;
50+
}
4751
}
4852
}
4953
return true;

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public class _533 {
4949
* 2. then we could go through the HashMap keyset:
5050
* if one row has N number of 'B's, we go through this row's each column to see if any element in this row is 'B' and also that element's column has N 'B's*/
5151
public int findBlackPixel(char[][] picture, int N) {
52-
if (picture == null || picture.length == 0 || picture[0].length == 0) return 0;
52+
if (picture == null || picture.length == 0 || picture[0].length == 0) {
53+
return 0;
54+
}
5355
int m = picture.length;
5456
int n = picture[0].length;
5557
int[] cols = new int[n];
@@ -73,9 +75,13 @@ public int findBlackPixel(char[][] picture, int N) {
7375

7476
int answer = 0;
7577
for (String key : map.keySet()) {
76-
if (map.get(key) != N) continue;
78+
if (map.get(key) != N) {
79+
continue;
80+
}
7781
for (int i = 0; i < n; i++) {
78-
if (key.charAt(i) == 'B' && cols[i] == N) answer += N;
82+
if (key.charAt(i) == 'B' && cols[i] == N) {
83+
answer += N;
84+
}
7985
}
8086
}
8187
return answer;

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ public class _536 {
3232

3333
public static class Solution1 {
3434
public TreeNode str2tree(String s) {
35-
if (s.equals("")) return null;
35+
if (s.equals("")) {
36+
return null;
37+
}
3638
int firstParen = s.indexOf("(");
3739
int val = firstParen == -1 ? Integer.parseInt(s) : Integer.parseInt(s.substring(0, firstParen));
3840
TreeNode cur = new TreeNode(val);
39-
if (firstParen == -1) return cur;
41+
if (firstParen == -1) {
42+
return cur;
43+
}
4044
int start = firstParen;
4145
int leftParenCount = 0;
4246
for (int i = start; i < s.length(); i++) {

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ private int dfs(TreeNode root, int val) {
5151
public static class GenericSolution {
5252
//This solution is generic for both BST and regular binary trees
5353
public TreeNode convertBST(TreeNode root) {
54-
if (root == null) return root;
54+
if (root == null) {
55+
return root;
56+
}
5557
List<Integer> list = new ArrayList<>();
5658
putNodeToList(list, root);
5759
Collections.sort(list);
@@ -69,7 +71,9 @@ public TreeNode convertBST(TreeNode root) {
6971
}
7072

7173
private TreeNode generateResultRoot(TreeNode root, TreeMap<Integer, Integer> treeMap, TreeNode result) {
72-
if (root != null) result.val = treeMap.get(root.val) + root.val;
74+
if (root != null) {
75+
result.val = treeMap.get(root.val) + root.val;
76+
}
7377
if (root.left != null) {
7478
result.left = new TreeNode(0);
7579
generateResultRoot(root.left, treeMap, result.left);
@@ -82,9 +86,15 @@ private TreeNode generateResultRoot(TreeNode root, TreeMap<Integer, Integer> tre
8286
}
8387

8488
private void putNodeToList(List<Integer> list, TreeNode root) {
85-
if (root != null) list.add(root.val);
86-
if (root.left != null) putNodeToList(list, root.left);
87-
if (root.right != null) putNodeToList(list, root.right);
89+
if (root != null) {
90+
list.add(root.val);
91+
}
92+
if (root.left != null) {
93+
putNodeToList(list, root.left);
94+
}
95+
if (root.right != null) {
96+
putNodeToList(list, root.right);
97+
}
8898
}
8999
}
90100

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public int findMinDifference(List<String> timePoints) {
2424
int hour = Integer.valueOf(timeParts[0]);
2525
int minute = Integer.valueOf(timeParts[1]);
2626
int value = hour * 60 + minute;
27-
if (allTimePoints[value]) return 0;
27+
if (allTimePoints[value]) {
28+
return 0;
29+
}
2830
allTimePoints[value] = true;
2931
}
3032

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ public class _541 {
1818
public String reverseStr(String s, int k) {
1919
StringBuilder stringBuilder = new StringBuilder();
2020
for (int i = 0; i < s.length(); i = i + 2 * k) {
21-
if (s.length() >= (i + k)) stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse());
22-
else {
21+
if (s.length() >= (i + k)) {
22+
stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse());
23+
} else {
2324
stringBuilder.append(new StringBuilder(s.substring(i)).reverse());
2425
break;
2526
}
26-
if ((i + 2 * k) <= s.length()) stringBuilder.append(s.substring(i + k, i + 2 * k));
27-
else stringBuilder.append(s.substring(i + k));
27+
if ((i + 2 * k) <= s.length()) {
28+
stringBuilder.append(s.substring(i + k, i + 2 * k));
29+
} else {
30+
stringBuilder.append(s.substring(i + k));
31+
}
2832
}
2933
return stringBuilder.toString();
3034
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ public List<List<Integer>> updateMatrix(List<List<Integer>> matrix) {
4242
Deque<int[]> deque = new LinkedList<>();
4343
for (int i = 0; i < m; i++) {
4444
for (int j = 0; j < n; j++) {
45-
if (matrix.get(i).get(j) == 0) deque.offer(new int[]{i, j});
46-
else matrix.get(i).set(j, Integer.MAX_VALUE);
45+
if (matrix.get(i).get(j) == 0) {
46+
deque.offer(new int[]{i, j});
47+
} else {
48+
matrix.get(i).set(j, Integer.MAX_VALUE);
49+
}
4750
}
4851
}
4952

@@ -53,8 +56,9 @@ public List<List<Integer>> updateMatrix(List<List<Integer>> matrix) {
5356
for (int i = 0; i < dirs.length - 1; i++) {
5457
int nextRow = currentCell[0] + dirs[i];
5558
int nextCol = currentCell[1] + dirs[i + 1];
56-
if (nextRow < 0 || nextCol < 0 || nextRow >= m || nextCol >= n || matrix.get(nextRow).get(nextCol) <= matrix.get(currentCell[0]).get(currentCell[1]) + 1)
59+
if (nextRow < 0 || nextCol < 0 || nextRow >= m || nextCol >= n || matrix.get(nextRow).get(nextCol) <= matrix.get(currentCell[0]).get(currentCell[1]) + 1) {
5760
continue;
61+
}
5862
deque.offer(new int[]{nextRow, nextCol});
5963
matrix.get(nextRow).set(nextCol, matrix.get(currentCell[0]).get(currentCell[1]) + 1);
6064
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public int diameterOfBinaryTree(TreeNode root) {
2626
}
2727

2828
private int dfs(TreeNode root) {
29-
if (root == null) return 0;
29+
if (root == null) {
30+
return 0;
31+
}
3032
int left = dfs(root.left);
3133
int right = dfs(root.right);
3234
diameter = Math.max(diameter, left + right);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public String findContestMatch(int n) {
5858
left++;
5959
right--;
6060
}
61-
if (n == 2) return pairs.get(0);
61+
if (n == 2) {
62+
return pairs.get(0);
63+
}
6264
return generateFinal(pairs, n / 2);
6365
}
6466

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
public class _547 {
3939

4040
public int findCircleNum(int[][] M) {
41-
if (M == null || M.length == 0 || M[0].length == 0) return 0;
41+
if (M == null || M.length == 0 || M[0].length == 0) {
42+
return 0;
43+
}
4244
int m = M.length;//number of rows in this matrix
4345
UnionFind unionFind = new UnionFind(m);
4446
for (int i = 0; i < m; i++) {
@@ -73,7 +75,9 @@ public void union(int i, int j) {
7375
}
7476

7577
public int find(int[] ids, int i) {
76-
if (ids[i] == i) return i;
78+
if (ids[i] == i) {
79+
return i;
80+
}
7781
return find(ids, ids[i]);
7882
}
7983
}

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public class _548 {
2727

2828
public boolean splitArray_O_N_3(int[] nums) {
2929
//TODO: this one is failed by test4, probably some index wrong
30-
if (nums == null || nums.length == 0) return false;
30+
if (nums == null || nums.length == 0) {
31+
return false;
32+
}
3133
long[] previousSums = new long[nums.length + 1];
3234
for (int i = 1; i <= nums.length; i++) {
3335
previousSums[i] = previousSums[i - 1] + nums[i - 1];
@@ -38,21 +40,28 @@ public boolean splitArray_O_N_3(int[] nums) {
3840
long sum1 = previousSums[i] - previousSums[0];
3941
for (int j = i + 2; j <= n - 4; j++) {
4042
long sum2 = previousSums[j] - previousSums[i + 1];
41-
if (sum1 != sum2) break;
43+
if (sum1 != sum2) {
44+
break;
45+
}
4246
for (int k = j + 2; k <= n - 2; k++) {
4347
long sum3 = previousSums[k] - previousSums[j + 1];
44-
if (sum2 != sum3) break;
48+
if (sum2 != sum3) {
49+
break;
50+
}
4551
long sum4 = previousSums[n] - previousSums[k + 1];
46-
if (sum3 == sum4) return true;
52+
if (sum3 == sum4) {
53+
return true;
54+
}
4755
}
4856
}
4957
}
5058
return false;
5159
}
5260

5361
public boolean splitArray_O_N_2(int[] nums) {
54-
if (nums.length < 7)
62+
if (nums.length < 7) {
5563
return false;
64+
}
5665
int[] sum = new int[nums.length];
5766
sum[0] = nums[0];
5867
for (int i = 1; i < nums.length; i++) {
@@ -61,12 +70,14 @@ public boolean splitArray_O_N_2(int[] nums) {
6170
for (int j = 3; j < nums.length - 3; j++) {
6271
HashSet<Integer> set = new HashSet<>();
6372
for (int i = 1; i < j - 1; i++) {
64-
if (sum[i - 1] == sum[j - 1] - sum[i])
73+
if (sum[i - 1] == sum[j - 1] - sum[i]) {
6574
set.add(sum[i - 1]);
75+
}
6676
}
6777
for (int k = j + 2; k < nums.length - 1; k++) {
68-
if (sum[nums.length - 1] - sum[k] == sum[k - 1] - sum[j] && set.contains(sum[k - 1] - sum[j]))
78+
if (sum[nums.length - 1] - sum[k] == sum[k - 1] - sum[j] && set.contains(sum[k - 1] - sum[j])) {
6979
return true;
80+
}
7081
}
7182
}
7283
return false;

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,27 @@ public int longestConsecutive(TreeNode root) {
3434
}
3535

3636
private int[] longestPath(TreeNode root) {
37-
if (root == null) return new int[]{0, 0};
37+
if (root == null) {
38+
return new int[]{0, 0};
39+
}
3840
int increasing = 1;
3941
int decreasing = 1;
4042
if (root.left != null) {
4143
int[] left = longestPath(root.left);
42-
if (root.val == root.left.val + 1) decreasing = left[1] + 1;
43-
else if (root.val == root.left.val - 1) increasing = left[0] + 1;
44+
if (root.val == root.left.val + 1) {
45+
decreasing = left[1] + 1;
46+
} else if (root.val == root.left.val - 1) {
47+
increasing = left[0] + 1;
48+
}
4449
}
4550

4651
if (root.right != null) {
4752
int[] right = longestPath(root.right);
48-
if (root.val == root.right.val + 1) decreasing = Math.max(right[1] + 1, decreasing);
49-
else if (root.val == root.right.val - 1) increasing = Math.max(right[0] + 1, increasing);
53+
if (root.val == root.right.val + 1) {
54+
decreasing = Math.max(right[1] + 1, decreasing);
55+
} else if (root.val == root.right.val - 1) {
56+
increasing = Math.max(right[0] + 1, increasing);
57+
}
5058
}
5159

5260
max = Math.max(max, decreasing + increasing - 1);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ public boolean checkRecord(String s) {
2626
for (int i = 0; i < s.length(); i++) {
2727
if ( s.charAt(i) == 'A') {
2828
aCount++;
29-
if (aCount > 1) return false;
29+
if (aCount > 1) {
30+
return false;
31+
}
3032
} else if (s.charAt(i) == 'L') {
3133
int continuousLCount = 0;
3234
while (i < s.length() && s.charAt(i) == 'L') {
3335
i++;
3436
continuousLCount++;
35-
if (continuousLCount > 2) return false;
37+
if (continuousLCount > 2) {
38+
return false;
39+
}
3640
}
3741
i--;
3842
}

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ public int checkRecord(int n) {
3434
int[][][] f = new int[n + 1][2][3];
3535

3636
f[0] = new int[][]{{1, 1, 1}, {1, 1, 1}};
37-
for (int i = 1; i <= n; i++)
38-
for (int j = 0; j < 2; j++)
37+
for (int i = 1; i <= n; i++) {
38+
for (int j = 0; j < 2; j++) {
3939
for (int k = 0; k < 3; k++) {
4040
int val = f[i - 1][j][2]; // ...P
41-
if (j > 0) val = (val + f[i - 1][j - 1][2]) % MOD; // ...A
42-
if (k > 0) val = (val + f[i - 1][j][k - 1]) % MOD; // ...L
41+
if (j > 0) {
42+
val = (val + f[i - 1][j - 1][2]) % MOD; // ...A
43+
}
44+
if (k > 0) {
45+
val = (val + f[i - 1][j][k - 1]) % MOD; // ...L
46+
}
4347
f[i][j][k] = val;
4448
}
49+
}
50+
}
4551
return f[n][1][2];
4652
}
4753

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ public String optimalDivision(int[] nums) {
4141
sj.add("George").add("Sally").add("Fred");
4242
String desiredString = sj.toString();*/
4343

44-
if (nums.length == 1) return "" + nums[0];
45-
if (nums.length == 2) return nums[0] + "/" + nums[1];
44+
if (nums.length == 1) {
45+
return "" + nums[0];
46+
}
47+
if (nums.length == 2) {
48+
return nums[0] + "/" + nums[1];
49+
}
4650

4751
/**Tricky one: the solution is fixed: always wrap the one from the second until the last.
4852
* Another important thing to note that such way could work is that:

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ public int leastBricks(List<List<Integer>> wall) {
5252
for (int i = 0; i < row.size() - 1; i++) {
5353
//NOTE: i < row.size()-1
5454
sum += row.get(i);
55-
if (map.containsKey(sum)) map.put(sum, map.get(sum) + 1);
56-
else map.put(sum, 1);
55+
if (map.containsKey(sum)) {
56+
map.put(sum, map.get(sum) + 1);
57+
} else {
58+
map.put(sum, 1);
59+
}
5760
}
5861
}
5962
int result = wall.size();

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public String splitLoopedString(String[] strs) {
3434
for (int i = 0; i < strs.length; i++) {
3535
stringBuilder.setLength(0);
3636
String reverse = stringBuilder.append(strs[i]).reverse().toString();
37-
if (strs[i].compareTo(reverse) < 0) strs[i] = reverse;
37+
if (strs[i].compareTo(reverse) < 0) {
38+
strs[i] = reverse;
39+
}
3840
}
3941
String result = "";
4042
for (int i = 0; i < strs.length; i++) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public int nextGreaterElement(int n) {
2121
while (i >= 0 && digits[i + 1] <= digits[i]) {
2222
i--;
2323
}
24-
if (i < 0) return -1;
24+
if (i < 0) {
25+
return -1;
26+
}
2527
int j = digits.length - 1;
2628
while (j >= 0 && digits[j] <= digits[i]) {
2729
j--;

0 commit comments

Comments
 (0)