Skip to content

Commit 9bb4255

Browse files
refactor for format
1 parent 686fb3e commit 9bb4255

19 files changed

+91
-87
lines changed

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@
1111
1212
A = [3,2,1,0,4], return false.*/
1313
public class _55 {
14-
14+
1515
public static boolean canJump_greedy(int[] nums) {
1616
int farthest = nums[0];
17-
for(int i = 0; i < nums.length; i++){
18-
if(i <= farthest && nums[i]+i > farthest) {
17+
for (int i = 0; i < nums.length; i++) {
18+
if (i <= farthest && nums[i] + i > farthest) {
1919
//i <= farthest is to make sure that this current i is within the current range
2020
// nums[i]+i > farthest is to make sure that it's necessary to update farthest with current nums[i]+i
21-
farthest = nums[i]+i;
21+
farthest = nums[i] + i;
2222
}
2323
}
24-
return farthest >= nums.length-1;
24+
return farthest >= nums.length - 1;
2525
}
2626

2727
//this normal dp ends in TLE for extreme test cases
2828
public static boolean canJump_dp(int[] nums) {
2929
boolean[] can = new boolean[nums.length];
3030
can[0] = true;
31-
for(int i = 0; i < nums.length; i++){
31+
for (int i = 0; i < nums.length; i++) {
3232
int reach = nums[i];
33-
if(can[i]){
34-
for(int j = i+1; j < nums.length && j <= i+reach; j++){
33+
if (can[i]) {
34+
for (int j = i + 1; j < nums.length && j <= i + reach; j++) {
3535
can[j] = true;
3636
}
3737
}
3838
}
39-
return can[nums.length-1];
39+
return can[nums.length - 1];
4040
}
4141

42-
public static void main(String...strings){
42+
public static void main(String... strings) {
4343
// int[] nums = new int[]{1,2};
44-
int[] nums = new int[]{0,2,3};
44+
int[] nums = new int[]{0, 2, 3};
4545
System.out.println(canJump_greedy(nums));
4646
}
4747
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public int leastBricks(List<List<Integer>> wall) {
4949
Map<Integer, Integer> map = new HashMap();
5050
for (List<Integer> row : wall) {
5151
int sum = 0;
52-
for (int i = 0; i < row.size()-1; i++) {//NOTE: i < row.size()-1
52+
for (int i = 0; i < row.size() - 1; i++) {
53+
//NOTE: i < row.size()-1
5354
sum += row.get(i);
54-
if (map.containsKey(sum)) map.put(sum, map.get(sum)+1);
55+
if (map.containsKey(sum)) map.put(sum, map.get(sum) + 1);
5556
else map.put(sum, 1);
5657
}
5758
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public String splitLoopedString(String[] strs) {
4343
for (String str : new String[]{strs[i], reverse}) {
4444
for (int k = 0; k < str.length(); k++) {
4545
StringBuilder sb = new StringBuilder(str.substring(k));
46-
for (int j = i+1; j < strs.length; j++) {
46+
for (int j = i + 1; j < strs.length; j++) {
4747
sb.append(strs[j]);
4848
}
4949
for (int j = 0; j < i; j++) {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ public int nextGreaterElement(int n) {
2121
while (i >= 0 && digits[i + 1] <= digits[i]) {
2222
i--;
2323
}
24-
if(i<0) return -1;
24+
if (i < 0) return -1;
2525
int j = digits.length - 1;
2626
while (j >= 0 && digits[j] <= digits[i]) {
2727
j--;
2828
}
2929
swap(digits, i, j);
3030
reverse(digits, i + 1);
31-
try{
31+
try {
3232
return Integer.parseInt(new String(digits));
33-
} catch(Exception e){
33+
} catch (Exception e) {
3434
return -1;
3535
}
3636
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public String reverseWords(String s) {
1919
stringBuilder.append(sbUtils.reverse().toString());
2020
stringBuilder.append(" ");
2121
}
22-
stringBuilder.setLength(stringBuilder.length()-1);
22+
stringBuilder.setLength(stringBuilder.length() - 1);
2323
return stringBuilder.toString();
2424
}
2525

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class _56 {
1919

2020
public static List<Interval> merge(List<Interval> intervals) {
21-
if(intervals.size() <= 1) return intervals;
21+
if (intervals.size() <= 1) return intervals;
2222

2323
Collections.sort(intervals, new Comparator<Interval>() {
2424
@Override
@@ -28,10 +28,10 @@ public int compare(Interval o1, Interval o2) {
2828
});
2929

3030
List<Interval> result = new ArrayList();
31-
for(int i = 0; i < intervals.size(); i++){
31+
for (int i = 0; i < intervals.size(); i++) {
3232
int start = intervals.get(i).start;
3333
int end = intervals.get(i).end;
34-
while(i < intervals.size() && end >= intervals.get(i).start){
34+
while (i < intervals.size() && end >= intervals.get(i).start) {
3535
end = Math.max(end, intervals.get(i).end);
3636
i++;
3737
}
@@ -41,7 +41,7 @@ public int compare(Interval o1, Interval o2) {
4141
return result;
4242
}
4343

44-
public static void main(String[] args){
44+
public static void main(String[] args) {
4545
List<Interval> list = new ArrayList<Interval>();
4646
// //test case 1:
4747
// list.add(new Interval(2,3));
@@ -51,10 +51,10 @@ public static void main(String[] args){
5151
// list.add(new Interval(3,4));
5252

5353
//test case 2:
54-
list.add(new Interval(1,3));
55-
list.add(new Interval(2,6));
56-
list.add(new Interval(8,10));
57-
list.add(new Interval(15,18));
54+
list.add(new Interval(1, 3));
55+
list.add(new Interval(2, 6));
56+
list.add(new Interval(8, 10));
57+
list.add(new Interval(15, 18));
5858
CommonUtils.printList(merge(list));
5959
}
6060

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public int subarraySum(int[] nums, int k) {
3333
for (int i = 0; i < nums.length; i++) {
3434
sum += nums[i];
3535
if (preSum.containsKey(sum - k)) {
36-
result += preSum.get(sum-k);
36+
result += preSum.get(sum - k);
3737
}
3838
preSum.put(sum, preSum.getOrDefault(sum, 0) + 1);
3939
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public int[][] matrixReshape(int[][] nums, int r, int c) {
4141
if (nums == null || nums.length == 0) return nums;
4242
int m = nums.length;
4343
int n = nums[0].length;
44-
if (r*c > m*n) return nums;
44+
if (r * c > m * n) return nums;
4545
int k = 0;
4646
int[][] result = new int[r][c];
4747
for (int i = 0; i < r; i++) {
4848
for (int j = 0; j < c; j++, k++) {
49-
result[i][j] = nums[k/n][k%n];
49+
result[i][j] = nums[k / n][k % n];
5050
}
5151
}
5252
return result;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public boolean isSubtree(TreeNode s, TreeNode t) {
4747
if (s != null && t != null && s.val == t.val) isSubTree = isSameTree(s, t);
4848
if (isSubTree) return true;
4949
boolean isSubTreeLeft = false;
50-
if (s.left != null) isSubTreeLeft= isSubtree(s.left, t);
50+
if (s.left != null) isSubTreeLeft = isSubtree(s.left, t);
5151
if (isSubTreeLeft) return true;
5252
boolean isSubTreeRight = false;
5353
if (s.right != null) isSubTreeRight = isSubtree(s.right, t);
@@ -56,7 +56,7 @@ public boolean isSubtree(TreeNode s, TreeNode t) {
5656
}
5757

5858
private boolean isSameTree(TreeNode p, TreeNode q) {
59-
if(p == null || q == null) return p == q;
59+
if (p == null || q == null) return p == q;
6060
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
6161
}
6262
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public int distributeCandies(int[] candies) {
3131
for (int i = 0; i < candies.length; i++) {
3232
int val = candies[i];
3333
sisCount++;
34-
if (sisCount >= candies.length/2) return candies.length/2;
34+
if (sisCount >= candies.length / 2) return candies.length / 2;
3535
while (i < candies.length && candies[i] == val) {
3636
i++;
3737
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
public class _58 {
1212

1313
public int lengthOfLastWord(String s) {
14-
if(s == null || s.length() == 0) return 0;
14+
if (s == null || s.length() == 0) return 0;
1515
s = s.trim();
16-
int n = s.length()-1;
17-
while(n >= 0 && s.charAt(n) != ' '){
16+
int n = s.length() - 1;
17+
while (n >= 0 && s.charAt(n) != ' ') {
1818
n--;
1919
}
2020
return s.length() - n - 1;

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ public class _581 {
2828
* Time: O(n)
2929
* Space: O(1)*/
3030
public int findUnsortedSubarray(int[] nums) {
31-
int n = nums.length, start = -1, end = -2, min = nums[n-1], max = nums[0];
32-
for (int i=1;i<n;i++) {
31+
int n = nums.length, start = -1, end = -2, min = nums[n - 1], max = nums[0];
32+
for (int i = 1; i < n; i++) {
3333
max = Math.max(max, nums[i]);
34-
min = Math.min(min, nums[n-1-i]);
34+
min = Math.min(min, nums[n - 1 - i]);
3535
if (nums[i] < max) end = i;
36-
if (nums[n-1-i] > min) start = n-1-i;
36+
if (nums[n - 1 - i] > min) start = n - 1 - i;
3737
}
3838
return end - start + 1;
3939
}
4040

41-
/**Time: O(nlogn)
42-
* Space: O(n)*/
41+
/**
42+
* Time: O(nlogn)
43+
* Space: O(n)
44+
*/
4345
public int findUnsortedSubarray_sorting(int[] nums) {
4446
int[] clones = nums.clone();
4547
Arrays.sort(clones);
@@ -51,7 +53,7 @@ public int findUnsortedSubarray_sorting(int[] nums) {
5153
end = Math.max(end, i);
5254
}
5355
}
54-
return (end - start > 0) ? end-start+1 : 0;
56+
return (end - start > 0) ? end - start + 1 : 0;
5557
}
5658

5759
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public class _583 {
2020
public int minDistance(String word1, String word2) {
2121
int m = word1.length();
2222
int n = word2.length();
23-
int[][] dp = new int[m+1][n+1];
23+
int[][] dp = new int[m + 1][n + 1];
2424
for (int i = 1; i <= m; i++) {
2525
for (int j = 1; j <= n; j++) {
26-
dp[i][j] = word1.charAt(i-1) == word2.charAt(j-1) ? dp[i-1][j-1]+1 : Math.max(dp[i-1][j], dp[i][j-1]);
26+
dp[i][j] = word1.charAt(i - 1) == word2.charAt(j - 1) ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
2727
}
2828
}
29-
return m + n - 2*dp[m][n];
29+
return m + n - 2 * dp[m][n];
3030
}
3131

3232
}

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

+21-19
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,29 @@ public class _59 {
1818
public int[][] generateMatrix(int num) {
1919
int temp = num;
2020
int[][] fourEdges = new int[num][num];
21-
int value = 1;
21+
int value = 1;
2222
int i = 0, j = 0;
23-
if(num%2 == 0) {//when num is even
24-
while(i < num/2 && j < num/2 && temp >= 0) {
25-
/* Assign the top row */
26-
while(j < temp) {
23+
if (num % 2 == 0) {
24+
//when num is even
25+
while (i < num / 2 && j < num / 2 && temp >= 0) {
26+
/* Assign the top row */
27+
while (j < temp) {
2728
fourEdges[i][j] = value;
2829
j++;
2930
value++;
3031

3132
}
3233

3334
/* Assign the right column */
34-
while(i < temp - 1) {
35+
while (i < temp - 1) {
3536
i++;
3637
fourEdges[i][j - 1] = value;
3738
value++;
3839
}
3940
j = j - 2;
4041

4142
/* Assign the bottom row */
42-
while(j >= num - temp) {
43+
while (j >= num - temp) {
4344
fourEdges[i][j] = value;
4445
j--;
4546
value++;
@@ -48,7 +49,7 @@ public int[][] generateMatrix(int num) {
4849
j++;
4950

5051
/* Assign the left column */
51-
while(i > num - temp) {
52+
while (i > num - temp) {
5253
fourEdges[i][j] = value;
5354
i--;
5455
value++;
@@ -59,26 +60,27 @@ public int[][] generateMatrix(int num) {
5960
temp--;
6061
}
6162

62-
} else {//when num is odd
63-
while(i < num/2 && j < num/2 && temp >= 0) {
64-
/* Assign the top row */
65-
while(j < temp) {
63+
} else {
64+
//when num is odd
65+
while (i < num / 2 && j < num / 2 && temp >= 0) {
66+
/* Assign the top row */
67+
while (j < temp) {
6668
fourEdges[i][j] = value;
6769
j++;
6870
value++;
6971

7072
}
7173

7274
/* Assign the right column */
73-
while(i < temp - 1){
75+
while (i < temp - 1) {
7476
i++;
7577
fourEdges[i][j - 1] = value;
7678
value++;
7779
}
7880
j = j - 2;
7981

8082
/* Assign the bottom row */
81-
while(j >= num - temp){
83+
while (j >= num - temp) {
8284
fourEdges[i][j] = value;
8385
j--;
8486
value++;
@@ -87,7 +89,7 @@ public int[][] generateMatrix(int num) {
8789
j++;
8890

8991
/* Assign the left column */
90-
while(i > num - temp){
92+
while (i > num - temp) {
9193
fourEdges[i][j] = value;
9294
i--;
9395
value++;
@@ -97,13 +99,13 @@ public int[][] generateMatrix(int num) {
9799
j++;
98100
temp--;
99101
}
100-
fourEdges[num/2][num/2] = num*num;
102+
fourEdges[num / 2][num / 2] = num * num;
101103
}
102104

103-
for(int m = 0; m < num; m++){
104-
for(int n = 0; n < num; n++){
105+
for (int m = 0; m < num; m++) {
106+
for (int n = 0; n < num; n++) {
105107
System.out.print(fourEdges[m][n] + "\t");
106-
if((n+1) % num == 0) {
108+
if ((n + 1) % num == 0) {
107109
System.out.println();
108110
}
109111
}

0 commit comments

Comments
 (0)