Skip to content

Commit febbeb7

Browse files
refactor for format
1 parent 21b17c5 commit febbeb7

Some content is hidden

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

49 files changed

+378
-135
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public Cell(int row, int col, int height) {
4141
}
4242

4343
public int trapRainWater(int[][] heights) {
44-
if (heights == null || heights.length == 0 || heights[0].length == 0)
44+
if (heights == null || heights.length == 0 || heights[0].length == 0) {
4545
return 0;
46+
}
4647

4748
PriorityQueue<Cell> queue = new PriorityQueue<>(1, (a, b) -> a.height - b.height);
4849

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public void abbrGenerator(String target, int i, String tmp, int abbr, int num) {
108108
}
109109
return;
110110
}
111-
if (num <= 0) return;
111+
if (num <= 0) {
112+
return;
113+
}
112114
char cur = target.charAt(i);
113115
abbrGenerator(target, i + 1, abbr == 0 ? tmp + cur : tmp + abbr + cur, 0, abbr == 0 ? num - 1 : num - 2);
114116
abbrGenerator(target, i + 1, tmp, abbr + 1, num);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ public int numberOfArithmeticSlices(int[] A) {
3434
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
3535
len++;
3636
} else {
37-
if (len > 2) sum += calculateSlices(len);
37+
if (len > 2) {
38+
sum += calculateSlices(len);
39+
}
3840
len = 2;//reset it to 2
3941
}
4042
}
41-
if (len > 2) sum += calculateSlices(len);
43+
if (len > 2) {
44+
sum += calculateSlices(len);
45+
}
4246
return sum;
4347
}
4448

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public static class Solution1 {
1818
* reference: https://discuss.leetcode.com/topic/22976/my-accepted-java-solution
1919
*/
2020
public int trap(int[] height) {
21-
if (height == null || height.length <= 2) return 0;
21+
if (height == null || height.length <= 2) {
22+
return 0;
23+
}
2224

2325
int max = height[0];
2426
int maxIndex = 0;

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ public int strongPasswordChecker(String s) {
1919
int[] arr = new int[carr.length];
2020

2121
for (int i = 0; i < arr.length;) {
22-
if (Character.isLowerCase(carr[i]))
22+
if (Character.isLowerCase(carr[i])) {
2323
a = 0;
24-
if (Character.isUpperCase(carr[i]))
24+
}
25+
if (Character.isUpperCase(carr[i])) {
2526
A = 0;
26-
if (Character.isDigit(carr[i]))
27+
}
28+
if (Character.isDigit(carr[i])) {
2729
d = 0;
30+
}
2831

2932
int j = i;
30-
while (i < carr.length && carr[i] == carr[j]) i++;
33+
while (i < carr.length && carr[i] == carr[j]) {
34+
i++;
35+
}
3136
arr[j] = i - j;
3237
}
3338

@@ -42,7 +47,9 @@ public int strongPasswordChecker(String s) {
4247

4348
for (int k = 1; k < 3; k++) {
4449
for (int i = 0; i < arr.length && overLen > 0; i++) {
45-
if (arr[i] < 3 || arr[i] % 3 != (k - 1)) continue;
50+
if (arr[i] < 3 || arr[i] % 3 != (k - 1)) {
51+
continue;
52+
}
4653
arr[i] -= Math.min(overLen, k);
4754
overLen -= k;
4855
}
@@ -55,7 +62,9 @@ public int strongPasswordChecker(String s) {
5562
overLen -= need;
5663
}
5764

58-
if (arr[i] >= 3) leftOver += arr[i] / 3;
65+
if (arr[i] >= 3) {
66+
leftOver += arr[i] / 3;
67+
}
5968
}
6069

6170
res += Math.max(totalMissing, leftOver);

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ public boolean validWordSquare(List<String> words) {
7575
for (int i = 0; i < words.size(); i++) {
7676
String word = words.get(i);
7777
for (int j = 0; j < word.length(); j++) {
78-
if (j >= words.size()) return false;
79-
if (i >= words.get(j).length()) return false;
80-
if (word.charAt(j) != words.get(j).charAt(i)) return false;
78+
if (j >= words.size()) {
79+
return false;
80+
}
81+
if (i >= words.get(j).length()) {
82+
return false;
83+
}
84+
if (word.charAt(j) != words.get(j).charAt(i)) {
85+
return false;
86+
}
8187
}
8288
}
8389
return true;

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

+30-10
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,36 @@ public String originalDigits(String s) {
3131
* we'll have to dedupe for counts[3], counts[5], counts[7] first before we dedupe counts[1] and counts[9].*/
3232
int[] counts = new int[10];
3333
for (int i = 0; i < s.length(); i++) {
34-
if (s.charAt(i) == 'o') counts[1]++;//2,4,0
35-
if (s.charAt(i) == 'w') counts[2]++;
36-
if (s.charAt(i) == 'h') counts[3]++;//8
37-
if (s.charAt(i) == 'u') counts[4]++;
38-
if (s.charAt(i) == 'f') counts[5]++;//4
39-
if (s.charAt(i) == 'x') counts[6]++;
40-
if (s.charAt(i) == 'v') counts[7]++;//5
41-
if (s.charAt(i) == 'g') counts[8]++;
42-
if (s.charAt(i) == 'i') counts[9]++;//5,6,8
43-
if (s.charAt(i) == 'z') counts[0]++;
34+
if (s.charAt(i) == 'o') {
35+
counts[1]++;//2,4,0
36+
}
37+
if (s.charAt(i) == 'w') {
38+
counts[2]++;
39+
}
40+
if (s.charAt(i) == 'h') {
41+
counts[3]++;//8
42+
}
43+
if (s.charAt(i) == 'u') {
44+
counts[4]++;
45+
}
46+
if (s.charAt(i) == 'f') {
47+
counts[5]++;//4
48+
}
49+
if (s.charAt(i) == 'x') {
50+
counts[6]++;
51+
}
52+
if (s.charAt(i) == 'v') {
53+
counts[7]++;//5
54+
}
55+
if (s.charAt(i) == 'g') {
56+
counts[8]++;
57+
}
58+
if (s.charAt(i) == 'i') {
59+
counts[9]++;//5,6,8
60+
}
61+
if (s.charAt(i) == 'z') {
62+
counts[0]++;
63+
}
4464
}
4565

4666
counts[3] -= counts[8];

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public int characterReplacement(String s, int k) {
5555
//this one can pass all test from test1 to test5, but tests like test6 won't pass
5656
public int characterReplacement_failed_attempt(String s, int k) {
5757
int longest = 0;
58-
if (s == null || s.length() == 0) return 0;
58+
if (s == null || s.length() == 0) {
59+
return 0;
60+
}
5961
for (int i = 0; i < s.length(); i++) {
6062
int count = 1;
6163
char val = s.charAt(i);

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class _43 {
1919
* Basically, the rule we can find is that products of each two digits will land in this position in the final product:
2020
* i+j and i+j+1*/
2121
public String multiply(String num1, String num2) {
22-
if (isZero(num1) || isZero(num2)) return "0";
22+
if (isZero(num1) || isZero(num2)) {
23+
return "0";
24+
}
2325
int[] a1 = new int[num1.length()];
2426
int[] a2 = new int[num2.length()];
2527
int[] product = new int[num1.length() + num2.length()];
@@ -42,15 +44,19 @@ public String multiply(String num1, String num2) {
4244

4345
StringBuilder stringBuilder = new StringBuilder();
4446
for (int i = 0; i < product.length; i++) {
45-
if (i == 0 && product[i] == 0) continue;
47+
if (i == 0 && product[i] == 0) {
48+
continue;
49+
}
4650
stringBuilder.append(product[i]);
4751
}
4852
return stringBuilder.toString();
4953
}
5054

5155
private boolean isZero(String num) {
5256
for (char c : num.toCharArray()) {
53-
if (c != '0') return false;
57+
if (c != '0') {
58+
return false;
59+
}
5460
}
5561
return true;
5662
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ public void inc(String key) {
6565
changeKey(key, 1);
6666
} else {
6767
keyCountMap.put(key, 1);
68-
if (head.next.count != 1)
68+
if (head.next.count != 1) {
6969
addBucketAfter(new Bucket(1), head);
70+
}
7071
head.next.keySet.add(key);
7172
countBucketMap.put(1, head.next);
7273
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
public class _434 {
1313

1414
public int countSegments(String s) {
15-
if (s == null || s.isEmpty()) return 0;
15+
if (s == null || s.isEmpty()) {
16+
return 0;
17+
}
1618
String[] segments = s.split(" ");
1719
int count = 0;
1820
for (String seg : segments) {
19-
if (seg.equals("")) continue;
21+
if (seg.equals("")) {
22+
continue;
23+
}
2024
count++;
2125
}
2226
return count;

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ public static int eraseOverlapIntervals(Interval[] intervals) {
4141
Collections.sort(Arrays.asList(intervals), new Comparator<Interval>() {
4242
@Override
4343
public int compare(Interval o1, Interval o2) {
44-
if (o1.end != o2.end) return o1.end - o2.end;
45-
else return o2.start - o1.start;
44+
if (o1.end != o2.end) {
45+
return o1.end - o2.end;
46+
} else {
47+
return o2.start - o1.start;
48+
}
4649
}
4750
});
4851
int end = Integer.MIN_VALUE;
4952
int count = 0;
5053
for (Interval interval : intervals) {
51-
if (interval.start >= end) end = interval.end;
52-
else count++;
54+
if (interval.start >= end) {
55+
end = interval.end;
56+
} else {
57+
count++;
58+
}
5359
}
5460
return count;
5561
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
public class _436 {
1212

1313
public static int[] findRightInterval(Interval[] intervals) {
14-
if (intervals == null || intervals.length == 0) return new int[0];
14+
if (intervals == null || intervals.length == 0) {
15+
return new int[0];
16+
}
1517
int[] result = new int[intervals.length];
1618
result[0] = -1;
1719
Interval last = intervals[intervals.length - 1];

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ The path does not need to start or end at the root or a leaf, but it must go dow
3333
public class _437 {
3434

3535
public int pathSum(TreeNode root, int sum) {
36-
if (root == null) return 0;
36+
if (root == null) {
37+
return 0;
38+
}
3739
return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
3840
}
3941

4042
private int pathSumFrom(TreeNode root, int sum) {
41-
if (root == null) return 0;
43+
if (root == null) {
44+
return 0;
45+
}
4246
return (root.val == sum ? 1 : 0) + pathSumFrom(root.left, sum - root.val) + pathSumFrom(root.right, sum - root.val);
4347
}
4448

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public class _438 {
3737
public List<Integer> findAnagrams(String s, String p) {
3838
List<Integer> result = new ArrayList();
3939
for (int i = 0; i <= s.length() - p.length(); i++) {
40-
if (isAnagram(s.substring(i, i + p.length()), p)) result.add(i);
40+
if (isAnagram(s.substring(i, i + p.length()), p)) {
41+
result.add(i);
42+
}
4143
}
4244
return result;
4345
}
@@ -50,7 +52,9 @@ private boolean isAnagram(String s, String p) {
5052
}
5153

5254
for (int i : c) {
53-
if (i != 0) return false;
55+
if (i != 0) {
56+
return false;
57+
}
5458
}
5559
return true;
5660
}
@@ -73,10 +77,14 @@ public List<Integer> findAnagrams(String s, String p) {
7377
hash[s.charAt(end) - 'a']--;
7478
end++;
7579

76-
if (count == 0) result.add(start);
80+
if (count == 0) {
81+
result.add(start);
82+
}
7783

7884
if ((end - start) == p.length()) {
79-
if (hash[s.charAt(start) - 'a'] >= 0) count++;
85+
if (hash[s.charAt(start) - 'a'] >= 0) {
86+
count++;
87+
}
8088
hash[s.charAt(start) - 'a']++;
8189
start++;
8290
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public String parseTernary(String expression) {
7474
stack.addFirst(tmpStack.pollFirst());
7575
}
7676
}
77-
if (stack.size() == 1) break;
77+
if (stack.size() == 1) {
78+
break;
79+
}
7880
}
7981
return Character.toString(stack.removeFirst());
8082
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
public class _441 {
3434

3535
public static int arrangeCoins(int n) {
36-
if (n < 2) return n;
36+
if (n < 2) {
37+
return n;
38+
}
3739
int row = 0;
3840
int count = 0;
3941
long sum = 0;
@@ -42,7 +44,9 @@ public static int arrangeCoins(int n) {
4244
sum += row;
4345
count++;
4446
}
45-
if (sum == n) return count;
47+
if (sum == n) {
48+
return count;
49+
}
4650
return count - 1;
4751
}
4852

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public List<Integer> findDuplicates_solution2(int[] nums) {
4040
List<Integer> result = new ArrayList();
4141
for (int i = 0; i < nums.length; i++) {
4242
int index = Math.abs(nums[i]) - 1;
43-
if (nums[index] < 0) result.add(Math.abs(index + 1));
43+
if (nums[index] < 0) {
44+
result.add(Math.abs(index + 1));
45+
}
4446
nums[index] = -nums[index];
4547
}
4648
return result;

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,18 @@ public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) {
9393
int index = 0;
9494
while (!queue.isEmpty()) {
9595
int size = queue.size();
96-
if (size > 1) return false;
96+
if (size > 1) {
97+
return false;
98+
}
9799
int curr = queue.poll();
98-
if (index == org.length || curr != org[index++]) return false;
100+
if (index == org.length || curr != org[index++]) {
101+
return false;
102+
}
99103
for (int next : map.get(curr)) {
100104
indegree.put(next, indegree.get(next) - 1);
101-
if (indegree.get(next) == 0) queue.offer(next);
105+
if (indegree.get(next) == 0) {
106+
queue.offer(next);
107+
}
102108
}
103109
}
104110
return index == org.length && index == map.size();

0 commit comments

Comments
 (0)