Skip to content

Commit 170e96d

Browse files
refactor for format
1 parent 65ff5ec commit 170e96d

File tree

12 files changed

+59
-21
lines changed

12 files changed

+59
-21
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ public class _653 {
4141

4242
public static class ListSolution {
4343
public boolean findTarget(TreeNode root, int k) {
44-
if (root == null) return false;
44+
if (root == null) {
45+
return false;
46+
}
4547
List<Integer> list = new ArrayList<>();
4648
dfs(root, list);
4749
for (int i = 0; i < list.size() - 1; i++) {
4850
for (int j = i + 1; j < list.size(); j++) {
49-
if (list.get(i) + list.get(j) == k) return true;
51+
if (list.get(i) + list.get(j) == k) {
52+
return true;
53+
}
5054
}
5155
}
5256
return false;
@@ -66,7 +70,9 @@ private void dfs(TreeNode root, List<Integer> list) {
6670
@Notes(todo = "This solution fails by _653Test.test6(), need to fix it.")
6771
public static class MapSolution {
6872
public boolean findTarget(TreeNode root, int k) {
69-
if (root == null) return false;
73+
if (root == null) {
74+
return false;
75+
}
7076
Map<Integer, Integer> map = new HashMap();//value is index
7177
int index = 0;
7278
preorder(root, map, index);

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public List<Integer> findClosestElements(List<Integer> arr, int k, int x) {
5454
}
5555

5656
private int findInsertPosition(List<Integer> arr, int x) {
57-
if (arr == null || arr.size() == 0) return 0;
57+
if (arr == null || arr.size() == 0) {
58+
return 0;
59+
}
5860
int len = arr.size();
5961
for (int i = 0; i < len; i++) {
6062
if (arr.get(0) > x) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public boolean isPossible(int[] nums) {
5252
freqMap.put(i+1, freqMap.get(i+1) - 1);
5353
freqMap.put(i+2, freqMap.get(i+2) - 1);
5454
appendFreqMap.put(i+3, appendFreqMap.getOrDefault(i+3, 0) + 1);
55-
} else return false;
55+
} else {
56+
return false;
57+
}
5658
freqMap.put(i, freqMap.get(i) - 1);
5759
}
5860
return true;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ For the point (1,1): floor(8/9) = floor(0.88888889) = 0
3030
*/
3131
public class _661 {
3232
public int[][] imageSmoother(int[][] M) {
33-
if (M == null || M.length == 0) return M;
33+
if (M == null || M.length == 0) {
34+
return M;
35+
}
3436
int m = M.length;
3537
int n = M[0].length;
3638
int[][] result = new int[m][n];

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public class _663 {
5959
public boolean checkEqualTree(TreeNode root) {
6060
Map<TreeNode, Integer> map = new HashMap<>();
6161
int totalSum = sumForEachNode(root, map);
62-
if (totalSum % 2 != 0 || map.size() < 2) return false;
62+
if (totalSum % 2 != 0 || map.size() < 2) {
63+
return false;
64+
}
6365
for (TreeNode key : map.keySet()) {
6466
if (map.get(key) == totalSum / 2) {
6567
return true;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public class _664 {
2424
/**reference: https://discuss.leetcode.com/topic/100137/java-solution-dp*/
2525
public int strangePrinter(String s) {
2626
int n = s.length();
27-
if (n == 0) return 0;
27+
if (n == 0) {
28+
return 0;
29+
}
2830

2931
int[][] dp = new int[101][101];
3032
for (int i = 0; i < n; i++) {

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ public String addBinary(String a, String b){
1717
StringBuilder sb = new StringBuilder();
1818
while(i >= 0 || j >= 0){
1919
int sum = carry;
20-
if(i >= 0) sum += a.charAt(i--) - '0';
21-
if(j >= 0) sum += b.charAt(j--) - '0';
20+
if(i >= 0) {
21+
sum += a.charAt(i--) - '0';
22+
}
23+
if(j >= 0) {
24+
sum += b.charAt(j--) - '0';
25+
}
2226
sb.append(sum%2);
2327
carry = sum/2;
2428
}
25-
if(carry != 0) sb.append(carry);
29+
if(carry != 0) {
30+
sb.append(carry);
31+
}
2632
return sb.reverse().toString();
2733
}
2834

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public class _68 {
3131

3232
public static List<String> fullJustify(String[] words, int L) {
3333
ArrayList<String> result = new ArrayList();
34-
if(words == null || words.length == 0)
34+
if(words == null || words.length == 0) {
3535
return result;
36+
}
3637
int count = 0;
3738
int last = 0;
3839
for(int i = 0; i < words.length; i++){

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ public int mySqrt(int x) {
1313
while(i <= j){
1414
long mid = (i+j)/2;
1515
long result = mid*mid;
16-
if(result == (long) x) return (int) mid;
17-
else if(result > x) j = mid-1;
18-
else i = mid+1;
16+
if(result == (long) x) {
17+
return (int) mid;
18+
} else if(result > x) {
19+
j = mid-1;
20+
} else {
21+
i = mid+1;
22+
}
1923
System.out.println(mid + " * " + mid + " = " + result + "\ti = " + i + "\tj = " + j);
2024
}
2125
return (int) j;

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ public int reverse_short_version(int x){
1010
while(x != 0){
1111
rev = rev*10 + x%10;
1212
x /= 10;
13-
if(rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) return 0;
13+
if(rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) {
14+
return 0;
15+
}
1416
}
1517
return (int) rev;
1618
}
1719

1820
public int reverse(int x) {
19-
if(x == 0) return 0;
21+
if(x == 0) {
22+
return 0;
23+
}
2024
//save the first bit if it's a negative sign
2125
StringBuilder sb = new StringBuilder();
2226
sb.append(x);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
public class _70 {
77
//classical dp problem
88
public int climbStairs(int n) {
9-
if(n < 1) return 0;
10-
if(n < 4) return n;
9+
if(n < 1) {
10+
return 0;
11+
}
12+
if(n < 4) {
13+
return n;
14+
}
1115
int[] dp = new int[n+1];
1216
//the number of ways to reach step n could be calculated from n-1 and n-2
1317
dp[1] = 1;

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public String simplifyPath(String path) {
2121
Deque<String> stack = new LinkedList<>();
2222
Set<String> skipSet = new HashSet<>(Arrays.asList("..", ".", ""));
2323
for (String dir : path.split("/")) {
24-
if (dir.equals("..") && !stack.isEmpty()) stack.pop();
25-
else if (!skipSet.contains(dir)) stack.push(dir);
24+
if (dir.equals("..") && !stack.isEmpty()) {
25+
stack.pop();
26+
} else if (!skipSet.contains(dir)) {
27+
stack.push(dir);
28+
}
2629
}
2730
String result = "";
2831
for (String dir : stack) {

0 commit comments

Comments
 (0)