8000 refactor for format · rrkandala/Leetcode@21b17c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21b17c5

Browse files
refactor for format
1 parent 5f28ec0 commit 21b17c5

File tree

18 files changed

+167
-61
lines changed

18 files changed

+167
-61
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,30 @@ public double findMedianSortedArrays(int[] A, int[] B) {
4141
}
4242

4343
public double getkth(int[] A, int aStart, int[] B, int bStart, int k) {
44-
if (aStart > A.length - 1) return B[bStart + k - 1];
45-
if (bStart > B.length - 1) return A[aStart + k - 1];
46-
if (k == 1) return Math.min(A[aStart], B[bStart]);
44+
if (aStart > A.length - 1) {
45+
return B[bStart + k - 1];
46+
}
47+
if (bStart > B.length - 1) {
48+
return A[aStart + k - 1];
49+
}
50+
if (k == 1) {
51+
return Math.min(A[aStart], B[bStart]);
52+
}
4753

4854
int aMid = Integer.MAX_VALUE;
4955
int bMid = Integer.MAX_VALUE;
50-
if (aStart + k / 2 - 1 < A.length) aMid = A[aStart + k / 2 - 1];
51-
if (bStart + k / 2 - 1 < B.length) bMid = B[bStart + k / 2 - 1];
56+
if (aStart + k / 2 - 1 < A.length) {
57+
aMid = A[aStart + k / 2 - 1];
58+
}
59+
if (bStart + k / 2 - 1 < B.length) {
60+
bMid = B[bStart + k / 2 - 1];
61+
}
5262

53-
if (aMid < bMid)
63+
if (aMid < bMid) {
5464
return getkth(A, aStart + k / 2, B, bStart, k - k / 2);// Check: aRight + bLeft
55-
else
65+
} else {
5666
return getkth(A, aStart, B, bStart + k / 2, k - k / 2);// Check: bRight + aLeft
67+
}
5768
}
5869
}
5970

@@ -98,9 +109,15 @@ public double findMedianSortedArrays(int[] A, int[] B, int K) {
98109
}
99110
}
100111

101-
if (highA == 0 && highB == 0) return 0;
102-
if (highA == 0) return B[highB - 1 - K];
103-
if (highB == 0) return A[highA - 1 - K];
112+
if (highA == 0 && highB == 0) {
113+
return 0;
114+
}
115+
if (highA == 0) {
116+
return B[highB - 1 - K];
117+
}
118+
if (highB == 0) {
119+
return A[highA - 1 - K];
120+
}
104121
return max(A[highA - 1], B[highB - 1]);
105122
}
106123
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public List<List<Integer>> combinationSum2(int[] candidates, int target) {
3232
void helper(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result) {
3333
if (target > 0) {
3434
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
35-
if (i > start && candidates[i] == candidates[i - 1])
35+
if (i > start && candidates[i] == candidates[i - 1]) {
3636
continue;//skip duplicates, this is one difference from Combination Sum I
37+
}
3738
curr.add(candidates[i]);
3839
helper(candidates, target - candidates[i], i + 1, curr, result);//i+1 is the other difference from Combination Sum I
3940
curr.remove(curr.size() - 1);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public List<String> readBinaryWatch(int num) {
2828
List<String> times = new ArrayList<>();
2929
for (int h = 0; h < 12; h++) {
3030
for (int m = 0; m < 60; m++) {
31-
if (Integer.bitCount(h * 60 + m) == num)
31+
if (Integer.bitCount(h * 60 + m) == num) {
3232
times.add(String.format("%d:%02d", h, m));//%02 means to pad this two-digit decimal number on the left with zeroes
33+
}
3334
}
3435
}
3536
return times;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public class _403 {
5353
/**Reference: https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-with-explanations/2
5454
* and https://leetcode.com/articles/frog-jump/#approach-5-using-dynamic-programmingaccepted*/
5555
public boolean canCross(int[] stones) {
56-
if (stones.length == 0) return true;
56+
if (stones.length == 0) {
57+
return true;
58+
}
5759
Map<Integer, Set<Integer>> map = new HashMap<>(stones.length);
5860
map.put(0, new HashSet<>());
5961
map.get(0).add(1);
@@ -65,7 +67,9 @@ public boolean canCross(int[] stones) {
6567
int stone = stones[i];
6668
for (int step : map.get(stone)) {
6769
int reach = step + stone;
68-
if (reach == stones[stones.length - 1]) return true;
70+
if (reach == stones[stones.length - 1]) {
71+
return true;
72+
}
6973
Set<Integer> set = map.get(reach);
7074
if (set != null) {
7175
set.add(step);

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
public class _404 {
1616
public int sumOfLeftLeaves(TreeNode root) {
1717
int result = 0;
18-
if (root == null) return result;
18+
if (root == null) {
19+
return result;
20+
}
1921
return dfs(root, result, false);
2022
}
2123

@@ -41,10 +43,15 @@ private class Solution2 {
4143

4244
public int sumOfLeftLeaves(TreeNode root) {
4345
int sum = 0;
44-
if (root == null) return sum;
46+
if (root == null) {
47+
return sum;
48+
}
4549
if (root.left != null) {
46-
if (root.left.left == null && root.left.right == null) sum += root.left.val;
47-
else sum += sumOfLeftLeaves(root.left);
50+
if (root.left.left == null && root.left.right == null) {
51+
sum += root.left.val;
52+
} else {
53+
sum += sumOfLeftLeaves(root.left);
54+
}
4855
}
4956
if (root.right != null) {
5057
sum += sumOfLeftLeaves(root.right);

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,36 @@
2323
public class _408 {
2424

2525
public boolean validWordAbbreviation(String word, String abbr) {
26-
if (abbr.length() > word.length()) return false;
27-
else {
26+
if (abbr.length() > word.length()) {
27+
return false;
28+
} else {
2829
char[] abbrChars = abbr.toCharArray();
2930
char[] wordChars = word.toCharArray();
3031
if (abbr.length() == word.length()) {
3132
boolean prevDigit = false;
3233
for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++, j++) {
3334
if (Character.isDigit(abbrChars[i]) && !prevDigit) {
3435
prevDigit = true;
35-
if (Character.getNumericValue(abbrChars[i]) != 1) return false;
36+
if (Character.getNumericValue(abbrChars[i]) != 1) {
37+
return false;
38+
}
3639
} else if (Character.isDigit(abbrChars[i]) && prevDigit) {
3740
return false;
38-
} else if (abbrChars[i] != wordChars[j]) return false;
39-
else if (prevDigit) prevDigit = false;
41+
} else if (abbrChars[i] != wordChars[j]) {
42+
return false;
43+
} else if (prevDigit) {
44+
prevDigit = false;
45+
}
4046
}
4147
return true;
4248
} else {
4349
StringBuilder stringBuilder = new StringBuilder();
4450
boolean firstDigit = true;
4551
for (int i = 0, j = 0; i < abbrChars.length && j < wordChars.length; i++) {
4652
while (i < abbrChars.length && Character.isDigit(abbrChars[i])) {
47-
if (firstDigit && Character.getNumericValue(abbrChars[i]) == 0) return false;
53+
if (firstDigit && Character.getNumericValue(abbrChars[i]) == 0) {
54+
return false;
55+
}
4856
stringBuilder.append(abbrChars[i]);
4957
i++;
5058
firstDigit = false;
@@ -55,10 +63,15 @@ public boolean validWordAbbreviation(String word, String abbr) {
5563
j += number;
5664
stringBuilder.setLength(0);
5765
}
58-
if ((i >= abbrChars.length && j < wordChars.length) || (i < abbrChars.length && j >= wordChars.length))
66+
if ((i >= abbrChars.length && j < wordChars.length) || (i < abbrChars.length && j >= wordChars.length)) {
67+
return false;
68+
}
69+
if (i < abbrChars.length && j < wordChars.length && abbrChars[i] != wordChars[j]) {
5970
return false;
60-
if (i < abbrChars.length && j < wordChars.length && abbrChars[i] != wordChars[j]) return false;
61-
if (j > wordChars.length && i <= abbrChars.length) return false;
71+
}
72+
if (j > wordChars.length && i <= abbrChars.length) {
73+
return false;
74+
}
6275
j++;
6376
}
6477
return true;

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public String minAbbreviation(String target, String[] dictionary) {
4747
abbrs = new ArrayList<>();
4848
abbrGenerator(target, 0, "", 0, i + 1);
4949
for (String s : abbrs) {
50-
if (search(s, root, 0, 0) == false) return s;
50+
if (search(s, root, 0, 0) == false) {
51+
return s;
52+
}
5153
}
5254
}
5355
return "";
@@ -66,16 +68,22 @@ public void addTrie(String s) {
6668
}
6769

6870
public boolean search(String target, Trie root, int i, int loop) {
69-
if (root == null) return false;
71+
if (root == null) {
72+
return false;
73+
}
7074

7175
if (loop != 0) {
7276
for (int a = 0; a < 26; a++) {
73-
if (search(target, root.children[a], i, loop - 1)) return true;
77+
if (search(target, root.children[a], i, loop - 1)) {
78+
return true;
79+
}
7480
}
7581
return false;
7682
}
7783
if (i == target.length()) {
78-
if (root.isWord) return true;
84+
if (root.isWord) {
85+
return true;
86+
}
7987
return false;
8088
}
8189
if (Character.isDigit(target.charAt(i))) {
@@ -92,8 +100,12 @@ public boolean search(String target, Trie root, int i, int loop) {
92100

93101
public void abbrGenerator(String target, int i, String tmp, int abbr, int num) {
94102
if (i == target.length()) {
95-
if (num == 0 && abbr == 0) abbrs.add(tmp);
96-
if (num == 1 && abbr != 0) abbrs.add(tmp + abbr);
103+
if (num == 0 && abbr == 0) {
104+
abbrs.add(tmp);
105+
}
106+
if (num == 1 && abbr != 0) {
107+
abbrs.add(tmp + abbr);
108+
}
97109
return;
98110
}
99111
if (num <= 0) return;

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ public int thirdMax(int[] nums) {
3636
max1 = Math.max(max1, i);
3737
}
3838
for (int i : nums) {
39-
if (i == max1) continue;
39+
if (i == max1) {
40+
continue;
41+
}
4042
max2 = Math.max(max2, i);
4143
}
4244
for (int i : nums) {
43-
if (i == max1 || i == max2) continue;
45+
if (i == max1 || i == max2) {
46+
continue;
47+
}
4448
max3 = Math.max(max3, i);
4549
}
4650
return (int) (max3 == Long.MIN_VALUE ? max1 : max3);

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
public class _415 {
1414

1515
public static String addStrings(String num1, String num2) {
16-
if (num1 == null || num1.length() == 0) return num2;
17-
else if (num2 == null || num2.length() == 0) return num1;
16+
if (num1 == null || num1.length() == 0) {
17+
return num2;
18+
} else if (num2 == null || num2.length() == 0) {
19+
return num1;
20+
}
1821

1922
int i = num1.length() - 1;
2023
int j = num2.length() - 1;
@@ -25,12 +28,18 @@ public static String addStrings(String num1, String num2) {
2528
char[] char2 = num2.toCharArray();
2629
while (i >= 0 || j >= 0) {
2730
sum = carry;
28-
if (i >= 0) sum += Character.getNumericValue(char1[i--]);
29-
if (j >= 0) sum += Character.getNumericValue(char2[j--]);
31+
if (i >= 0) {
32+
sum += Character.getNumericValue(char1[i--]);
33+
}
34+
if (j >= 0) {
35+
sum += Character.getNumericValue(char2[j--]);
36+
}
3037
carry = sum / 10;
3138
sb.append(sum % 10);
3239
}
33-
if (carry != 0) sb.append(carry);
40+
if (carry != 0) {
41+
sb.append(carry);
42+
}
3443

3544
return sb.reverse().toString();
3645
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public boolean canPartition(int[] nums) {
3939
sum += num;
4040
}
4141

42-
if ((sum & 1) == 1) return false;
42+
if ((sum & 1) == 1) {
43+
return false;
44+
}
4345

4446
sum /= 2;
4547

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class _417 {
2525
public List<int[]> pacificAtlantic(int[][] matrix) {
2626

2727
List<int[]> result = new ArrayList();
28-
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return result;
28+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
29+
return result;
30+
}
2931

3032
int m = matrix.length;
3133
int n = matrix[0].length;
@@ -56,7 +58,9 @@ public List<int[]> pacificAtlantic(int[][] matrix) {
5658
void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) {
5759
int m = matrix.length;
5860
int n = matrix[0].length;
59-
if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) return;
61+
if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) {
62+
return;
63+
}
6064
visited[x][y] = true;
6165
dfs(matrix, visited, matrix[x][y], x + 1, y);
6266
dfs(matrix, visited, matrix[x][y], x, y + 1);

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,23 @@ public class _419 {
3434
* This is achieved by counting cells that don't have 'X' to the left and above them.
3535
*/
3636
public int countBattleships_no_modify_original_input(char[][] board) {
37-
if (board == null || board.length == 0) return 0;
37+
if (board == null || board.length == 0) {
38+
return 0;
39+
}
3840
int count = 0;
3941
int m = board.length;
4042
int n = board[0].length;
4143
for (int i = 0; i < m; i++) {
4244
for (int j = 0; j < n; j++) {
43-
if (board[i][j] == '.') continue;//if it can pass this line, then board[i][j] must be 'X'
44-
if (j > 0 && board[i][j - 1] == 'X') continue;//then we check if its left is 'X'
45-
if (i > 0 && board[i - 1][j] == 'X') continue;//also check if its top is 'X'
45+
if (board[i][j] == '.') {
46+
continue;//if it can pass this line, then board[i][j] must be 'X'
47+
}
48+
if (j > 0 && board[i][j - 1] == 'X') {
49+
continue;//then we check if its left is 'X'
50+
}
51+
if (i > 0 && board[i - 1][j] == 'X') {
52+
continue;//also check if its top is 'X'
53+
}
4654
count++;
4755
}
4856
}
@@ -53,7 +61,9 @@ public int countBattleships_no_modify_original_input(char[][] board) {
5361
* My original solution, actually modified the input. I just undo it at the end.
5462
*/
5563
public int countBattleships(char[][] board) {
56-
if (board == null || board.length == 0) return 0;
64+
if (board == null || board.length == 0) {
65+
return 0;
66+
}
5767
int result = 0;
5868
int m = board.length;
5969
int n = board[0].length;
@@ -68,15 +78,21 @@ public int countBattleships(char[][] board) {
6878

6979
for (int i = 0; i < m; i++) {
7080
for (int j = 0; j < n; j++) {
71-
if (board[i][j] == '#') board[i][j] = 'X';
81+
if (board[i][j] == '#') {
82+
board[i][j] = 'X';
83+
}
7284
}
7385
}
7486
return result;
7587
}
7688

7789
private void dfs(char[][] board, int x, int y, int m, int n) {
78-
if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'X') return;
79-
if (board[x][y] == 'X') board[x][y] = '#';
90+
if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'X') {
91+
return;
92+
}
93+
if (board[x][y] == 'X') {
94+
board[x][y] = '#';
95+
}
8096
dfs(board, x + 1, y, m, n);
8197
dfs(board, x, y + 1, m, n);
8298
dfs(board, x - 1, y, m, n);

0 commit comments

Comments
 (0)
0