Skip to content

Commit 981ef79

Browse files
refactor for format
1 parent e2eec22 commit 981ef79

File tree

25 files changed

+167
-61
lines changed

25 files changed

+167
-61
lines changed

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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 15 additions & 5 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)