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<String> 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<Integer> 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
}

0 commit comments

Comments
 (0)