Skip to content

Commit 45c398e

Browse files
refactor for format
1 parent d9d7017 commit 45c398e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+410
-163
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ public int lengthOfLIS(int[] nums) {
4444
* that this guarantees that the return value will be >= 0 if
4545
* and only if the key is found.*/
4646
int index = Arrays.binarySearch(dp, 0, len, x);
47-
if (index < 0) index = -(index + 1);
47+
if (index < 0) {
48+
index = -(index + 1);
49+
}
4850
dp[index] = x;
49-
if (index == len) len++;
51+
if (index == len) {
52+
len++;
53+
}
5054
}
5155
return len;
5256
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public class _301 {
2121

2222
public List<String> removeInvalidParentheses(String s) {
2323
List<String> result = new ArrayList<>();
24-
if (s == null) return result;
24+
if (s == null) {
25+
return result;
26+
}
2527

2628
Set<String> visited = new HashSet();
2729
Queue<String> q = new LinkedList();
@@ -38,12 +40,14 @@ public List removeInvalidParentheses(String s) {
3840
result.add(curr);
3941
}
4042

41-
if (found)
43+
if (found) {
4244
continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result
45+
}
4346

4447
for (int i = 0; i < curr.length(); i++) {
45-
if (curr.charAt(i) != '(' && curr.charAt(i) != ')')
48+
if (curr.charAt(i) != '(' && curr.charAt(i) != ')') {
4649
continue;//this is to rule out those non-parentheses characters
50+
}
4751

4852
String next = curr.substring(0, i) + curr.substring(i + 1);
4953
if (!visited.contains(next)) {
@@ -61,10 +65,14 @@ private boolean isValid(String str) {
6165
int count = 0;
6266
for (int i = 0; i < chars.length; i++) {
6367
char c = chars[i];
64-
if (c == '(') count++;
68+
if (c == '(') {
69+
count++;
70+
}
6571
if (c == ')') {
6672
count--;
67-
if (count == -1) return false;
73+
if (count == -1) {
74+
return false;
75+
}
6876
}
6977
}
7078
return count == 0;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ private int searchRows(int i, int j, int left, int right, boolean opt) {
4848
while (i != j) {
4949
int k = left;
5050
int mid = (i + j) / 2;
51-
while (k < right && image[mid][k] == '0') ++k;
51+
while (k < right && image[mid][k] == '0') {
52+
++k;
53+
}
5254
if (k < right == opt) {
5355
j = mid;
5456
} else {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public NumArray(int[] nums) {
2727
}
2828

2929
public int sumRange(int i, int j) {
30-
if (i == 0) return sums[j];
30+
if (i == 0) {
31+
return sums[j];
32+
}
3133
return sums[j] - sums[i - 1];
3234
}
3335
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public class _304 {
2828
public class NumMatrix {
2929

3030
public NumMatrix(int[][] matrix) {
31-
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
31+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
3232
return;
33+
}
3334

3435
/**The dimensions of this tot matrix is actually 1 bigger than the given matrix, cool!*/
3536
tot = new int[matrix.length + 1][matrix[0].length + 1];

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public class _305 {
4444

4545
public int find(int[] father, int id) {
4646
int tf = father[id];
47-
while (tf != father[tf])
47+
while (tf != father[tf]) {
4848
tf = father[tf];
49+
}
4950
int cur = id;
5051
int tmp;
5152
while (father[cur] != tf) {
@@ -71,12 +72,14 @@ public void union(int[] father, int[] sz, int id1, int id2) {
7172
}
7273

7374
public List<Integer> numIslands2(int m, int n, int[][] positions) {
74-
if (m == 0 || n == 0)
75-
return new ArrayList<Integer>();
76-
ArrayList<Integer> res = new ArrayList<Integer>();
75+
if (m == 0 || n == 0) {
76+
return new ArrayList<>();
77+
}
78+
ArrayList<Integer> res = new ArrayList();
7779
int[] father = new int[m * n];
78-
for (int i = 0; i < father.length; i++)
80+
for (int i = 0; i < father.length; i++) {
7981
father[i] = -1;
82+
}
8083
int[] sz = new int[m * n];
8184
int[] dr = { 0, 0, -1, 1 };
8285
int[] dc = { -1, 1, 0, 0 };

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,31 @@ public boolean isAdditiveNumber(String num) {
2424
int n = num.length();
2525
for (int i = 1; i <= n / 2; ++i) {
2626
for (int j = 1; Math.max(j, i) <= n - i - j; ++j) {
27-
if (isValid(i, j, num)) return true;
27+
if (isValid(i, j, num)) {
28+
return true;
29+
}
2830
}
2931
}
3032
return false;
3133
}
3234

3335
private boolean isValid(int i, int j, String num) {
34-
if (num.charAt(0) == '0' && i > 1) return false;
35-
if (num.charAt(i) == '0' && j > 1) return false;
36+
if (num.charAt(0) == '0' && i > 1) {
37+
return false;
38+
}
39+
if (num.charAt(i) == '0' && j > 1) {
40+
return false;
41+
}
3642
String sum;
3743
Long x1 = Long.parseLong(num.substring(0, i));
3844
Long x2 = Long.parseLong(num.substring(i, i + j));
3945
for (int start = i + j; start != num.length(); start += sum.length()) {
4046
x2 = x2 + x1;
4147
x1 = x2 - x1;
4248
sum = x2.toString();
43-
if (!num.startsWith(sum, start)) return false;
49+
if (!num.startsWith(sum, start)) {
50+
return false;
51+
}
4452
}
4553
return true;
4654
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public class NumMatrix {
3232
int width;
3333

3434
public NumMatrix(int[][] matrix) {
35-
if (matrix.length == 0 || matrix[0].length == 0) return;
35+
if (matrix.length == 0 || matrix[0].length == 0) {
36+
return;
37+
}
3638
height = matrix.length;
3739
width = matrix[0].length;
3840
this.nums = new int[height][width];
@@ -45,7 +47,9 @@ public NumMatrix(int[][] matrix) {
4547
}
4648

4749
public void update(int rowIndex, int colIndex, int newVal) {
48-
if (height == 0 || width == 0) return;
50+
if (height == 0 || width == 0) {
51+
return;
52+
}
4953
int delta = newVal - nums[rowIndex][colIndex];
5054
nums[rowIndex][colIndex] = newVal;
5155
for (int i = rowIndex + 1; i <= height; i += i & (-i)) {
@@ -56,7 +60,9 @@ public void update(int rowIndex, int colIndex, int newVal) {
5660
}
5761

5862
public int sumRegion(int row1, int col1, int row2, int col2) {
59-
if (height == 0 || width == 0) return 0;
63+
if (height == 0 || width == 0) {
64+
return 0;
65+
}
6066
return sum(row2 + 1, col2 + 1) + sum(row1, col1) - sum(row1, col2 + 1) - sum(row2 + 1, col1);
6167
}
6268

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ static class solutionWithTheSecondHighestUpvotes {
121121
* We cannot sell. The max profit at i = 0 ending with a sell is 0.
122122
*/
123123
public int maxProfit(int[] prices) {
124-
if (prices == null || prices.length <= 1)
124+
if (prices == null || prices.length <= 1) {
125125
return 0;
126+
}
126127

127128
int b0 = -prices[0];
128129
int b1 = b0;

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,30 @@ public List findMinHeightTrees(int n, int[][] edges) {
5656
}
5757

5858
List<Set<Integer>> adj = new ArrayList<>(n);
59-
for (int i = 0; i < n; ++i)
59+
for (int i = 0; i < n; ++i) {
6060
adj.add(new HashSet<>());
61+
}
6162
for (int[] edge : edges) {
6263
adj.get(edge[0]).add(edge[1]);
6364
adj.get(edge[1]).add(edge[0]);
6465
}
6566

6667
List<Integer> leaves = new ArrayList<>();
67-
for (int i = 0; i < n; ++i)
68-
if (adj.get(i).size() == 1)
68+
for (int i = 0; i < n; ++i) {
69+
if (adj.get(i).size() == 1) {
6970
leaves.add(i);
71+
}
72+
}
7073

7174
while (n > 2) {
7275
n -= leaves.size();
7376
List<Integer> newLeaves = new ArrayList<>();
7477
for (int i : leaves) {
7578
int j = adj.get(i).iterator().next();
7679
adj.get(j).remove(i);
77-
if (adj.get(j).size() == 1)
80+
if (adj.get(j).size() == 1) {
7881
newLeaves.add(j);
82+
}
7983
}
8084
leaves = newLeaves;
8185
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public int[][] multiply(int[][] A, int[][] B) {
3232
for (int j = 0; j < n; j++) {
3333
if (A[i][j] != 0) {
3434
for (int k = 0; k < p; k++) {
35-
if (B[j][k] != 0) C[i][k] += A[i][j] * B[j][k];
35+
if (B[j][k] != 0) {
36+
C[i][k] += A[i][j] * B[j][k];
37+
}
3638
}
3739
}
3840
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ public class _312 {
2323
public int maxCoins(int[] iNums) {
2424
int[] nums = new int[iNums.length + 2];
2525
int n = 1;
26-
for (int x : iNums)
27-
if (x > 0)
26+
for (int x : iNums) {
27+
if (x > 0) {
2828
nums[n++] = x;
29+
}
30+
}
2931
nums[0] = nums[n++] = 1;
3032

33+
3134
int[][] dp = new int[n][n];
32-
for (int k = 2; k < n; ++k)
35+
for (int k = 2; k < n; ++k) {
3336
for (int left = 0; left < n - k; ++left) {
3437
int right = left + k;
35-
for (int i = left + 1; i < right; ++i)
38+
for (int i = left + 1; i < right; ++i) {
3639
dp[left][right] = Math.max(dp[left][right],
3740
nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right]);
41+
}
3842
}
39-
43+
}
4044
return dp[0][n - 1];
4145
}
4246

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
public class _314 {
7070
public List<List<Integer>> verticalOrder_using_treemap(TreeNode root) {
7171
List<List<Integer>> result = new ArrayList();
72-
if (root == null) return result;
72+
if (root == null) {
73+
return result;
74+
}
7375
Queue<TreeNode> bfsQ = new LinkedList();
7476
Queue<Integer> indexQ = new LinkedList();
7577
TreeMap<Integer, List<Integer>> map = new TreeMap();
@@ -105,7 +107,9 @@ public List> verticalOrder_using_treemap(TreeNode root) {
105107

106108
public List<List<Integer>> verticalOrder_using_hashmap(TreeNode root) {
107109
List<List<Integer>> result = new ArrayList();
108-
if (root == null) return result;
110+
if (root == null) {
111+
return result;
112+
}
109113
Queue<TreeNode> bfsQ = new LinkedList();
110114
Queue<Integer> indexQ = new LinkedList();
111115
HashMap<Integer, List<Integer>> map = new HashMap();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public class _317 {
2727

2828
public int shortestDistance(int[][] grid) {
2929
int m = grid.length;
30-
if (m == 0) return -1;
30+
if (m == 0) {
31+
return -1;
32+
}
3133
int n = grid[0].length;
3234
int[][] reach = new int[m][n];
3335
int[][] distance = new int[m][n];

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fishercoder.solutions;
22

33
import java.util.Arrays;
4-
import java.util.Comparator;
54
import java.util.HashSet;
65
import java.util.Set;
76

@@ -29,7 +28,9 @@ public class _318 {
2928
//this is a big hint that we could use integer (total 32 bits) to represent each char
3029
//values[i] means how many unique characters this string words[i] has
3130
public int maxProduct(String[] words) {
32-
if (words == null || words.length == 0) return 0;
31+
if (words == null || words.length == 0) {
32+
return 0;
33+
}
3334
int len = words.length;
3435
int[] values = new int[len];
3536
for (int i = 0; i < words.length; i++) {
@@ -54,21 +55,26 @@ public int maxProduct(String[] words) {
5455
public int maxProduct_with_pruning(String[] words) {
5556
int maxProduct = 0;
5657
//use a customized comparator to make the words list sorted in descending order, brilliant!
57-
Arrays.sort(words, new Comparator<String>() {
58-
@Override
59-
public int compare(String o1, String o2) {
60-
if (o1.length() > o2.length()) return -1;
61-
else if (o1.length() < o2.length()) return 1;
62-
else return 0;
58+
Arrays.sort(words, (o1, o2) -> {
59+
if (o1.length() > o2.length()) {
60+
return -1;
61+
} else if (o1.length() < o2.length()) {
62+
return 1;
63+
} else {
64+
return 0;
6365
}
6466
});
6567
for (int i = 0; i < words.length - 1; i++) {
6668
String currWord = words[i];
6769
int currWordLen = currWord.length();
68-
if (maxProduct > currWordLen * words[i + 1].length()) break;//pruning
70+
if (maxProduct > currWordLen * words[i + 1].length()) {
71+
break;//pruning
72+
}
6973
char[] chars = currWord.toCharArray();
7074
Set<Character> set = new HashSet();
71-
for (char c : chars) set.add(c);
75+
for (char c : chars) {
76+
set.add(c);
77+
}
7278
for (int j = i + 1; j < words.length; j++) {
7379
char[] chars2 = words[j].toCharArray();
7480
boolean valid = true;
@@ -98,7 +104,9 @@ public int maxProduct_most_brute_force(String[] words) {
98104
int currWordLen = currWord.length();
99105
char[] chars = currWord.toCharArray();
100106
Set<Character> set = new HashSet();
101-
for (char c : chars) set.add(c);
107+
for (char c : chars) {
108+
set.add(c);
109+
}
102110
for (int j = i + 1; j < words.length; j++) {
103111
char[] chars2 = words[j].toCharArray();
104112
boolean valid = true;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
public class _319 {
2424

2525
public int bulbSwitch(int n) {
26-
if (n < 2) return n;
26+
if (n < 2) {
27+
return n;
28+
}
2729
return (int) Math.sqrt(n);
2830
}
2931

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public List generateAbbreviations(String word) {
2121

2222
private void backtrack(String word, List<String> result, int position, String current, int count) {
2323
if (position == word.length()) {
24-
if (count > 0) current += count;
24+
if (count > 0) {
25+
current += count;
26+
}
2527
result.add(current);
2628
} else {
2729
backtrack(word, result, position + 1, current, count + 1);

0 commit comments

Comments
 (0)