Skip to content

Commit ae7e732

Browse files
refactor for format
1 parent bfbdf99 commit ae7e732

Some content is hidden

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

61 files changed

+411
-383
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public double getkth(int[] A, int aStart, int[] B, int bStart, int k) {
4545
if (k == 1) return Math.min(A[aStart], B[bStart]);
4646

4747
int aMid = Integer.MAX_VALUE, bMid = Integer.MAX_VALUE;
48-
if (aStart + k/2 - 1 < A.length) aMid = A[aStart + k/2 - 1];
49-
if (bStart + k/2 - 1 < B.length) bMid = B[bStart + k/2 - 1];
48+
if (aStart + k / 2 - 1 < A.length) aMid = A[aStart + k / 2 - 1];
49+
if (bStart + k / 2 - 1 < B.length) bMid = B[bStart + k / 2 - 1];
5050

5151
if (aMid < bMid)
52-
return getkth(A, aStart + k/2, B, bStart, k - k/2);// Check: aRight + bLeft
52+
return getkth(A, aStart + k / 2, B, bStart, k - k / 2);// Check: aRight + bLeft
5353
else
54-
return getkth(A, aStart, B, bStart + k/2, k - k/2);// Check: bRight + aLeft
54+
return getkth(A, aStart, B, bStart + k / 2, k - k / 2);// Check: bRight + aLeft
5555
}
5656
}
5757

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,25 @@ public List<List<Integer>> combinationSum2(int[] candidates, int target) {
2828
helper(candidates, target, 0, new ArrayList(), result);
2929
return result;
3030
}
31-
32-
void helper(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result){
33-
if(target > 0){
34-
for(int i = start; i < candidates.length && target >= candidates[i]; i++){
35-
if(i > start && candidates[i] == candidates[i-1]) continue;//skip duplicates, this is one difference from Combination Sum I
31+
32+
void helper(int[] candidates, int target, int start, List<Integer> curr, List<List<Integer>> result) {
33+
if (target > 0) {
34+
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
35+
if (i > start && candidates[i] == candidates[i - 1])
36+
continue;//skip duplicates, this is one difference from Combination Sum I
3637
curr.add(candidates[i]);
37-
helper(candidates, target - candidates[i], i+1, curr, result);//i+1 is the other difference from Combination Sum I
38-
curr.remove(curr.size()-1);
38+
helper(candidates, target - candidates[i], i + 1, curr, result);//i+1 is the other difference from Combination Sum I
39+
curr.remove(curr.size() - 1);
3940
}
40-
} else if(target == 0){
41+
} else if (target == 0) {
4142
List<Integer> temp = new ArrayList(curr);
4243
result.add(temp);
4344
}
4445
}
4546

46-
public static void main(String...args){
47+
public static void main(String... args) {
4748
_40 test = new _40();
48-
int[] candidates = new int[]{10,1,2,7,6,1,5};
49+
int[] candidates = new int[]{10, 1, 2, 7, 6, 1, 5};
4950
int target = 8;
5051
List<List<Integer>> result = test.combinationSum2(candidates, target);
5152
CommonUtils.printListList(result);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ 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) times.add(String.format("%d:%02d", h, m));//%02 means to pad this two-digit decimal number on the left with zeroes
31+
if (Integer.bitCount(h * 60 + m) == num)
32+
times.add(String.format("%d:%02d", h, m));//%02 means to pad this two-digit decimal number on the left with zeroes
3233
}
3334
}
3435
return times;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public class _402 {
2828

2929
/**credit: https://discuss.leetcode.com/topic/59412/a-greedy-method-using-stack-o-n-time-and-o-n-space*/
3030
public String removeKdigits(String num, int k) {
31-
int digits = num.length()-k;
31+
int digits = num.length() - k;
3232
char[] stack = new char[num.length()];
3333
int top = 0;
3434

3535
for (int i = 0; i < num.length(); i++) {
3636
char c = num.charAt(i);
37-
while (top > 0 && stack[top-1] > c && k > 0) {
37+
while (top > 0 && stack[top - 1] > c && k > 0) {
3838
top--;
3939
k--;
4040
}
@@ -45,7 +45,7 @@ public String removeKdigits(String num, int k) {
4545
while (index < digits && stack[index] == '0') {
4646
index++;
4747
}
48-
return index == digits ? "0" : new String(stack, index, digits-index);
48+
return index == digits ? "0" : new String(stack, index, digits - index);
4949
}
5050

5151
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public boolean canCross(int[] stones) {
6565
int stone = stones[i];
6666
for (int step : map.get(stone)) {
6767
int reach = step + stone;
68-
if (reach == stones[stones.length-1]) return true;
68+
if (reach == stones[stones.length - 1]) return true;
6969
Set<Integer> set = map.get(reach);
7070
if (set != null) {
7171
set.add(step);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,38 @@
1515
public class _404 {
1616
public int sumOfLeftLeaves(TreeNode root) {
1717
int result = 0;
18-
if(root == null) return result;
18+
if (root == null) return result;
1919
return dfs(root, result, false);
2020
}
21-
21+
2222
private int dfs(TreeNode root, int result, boolean left) {
23-
if(root.left == null && root.right == null && left) {
23+
if (root.left == null && root.right == null && left) {
2424
result += root.val;
2525
return result;
2626
}
2727
int leftResult = 0;
28-
if(root.left != null){
28+
if (root.left != null) {
2929
left = true;
3030
leftResult = dfs(root.left, result, left);
3131
}
3232
int rightResult = 0;
33-
if(root.right != null){
33+
if (root.right != null) {
3434
left = false;
3535
rightResult = dfs(root.right, result, left);
3636
}
3737
return leftResult + rightResult;
3838
}
39-
39+
4040
private class Solution2 {
4141

4242
public int sumOfLeftLeaves(TreeNode root) {
4343
int sum = 0;
44-
if(root == null) return sum;
45-
if(root.left != null){
46-
if(root.left.left == null && root.left.right == null) sum += root.left.val;
44+
if (root == null) return sum;
45+
if (root.left != null) {
46+
if (root.left.left == null && root.left.right == null) sum += root.left.val;
4747
else sum += sumOfLeftLeaves(root.left);
4848
}
49-
if(root.right != null){
49+
if (root.right != null) {
5050
sum += sumOfLeftLeaves(root.right);
5151
}
5252
return sum;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@
2727
*/
2828
public class _406 {
2929

30-
/**Credit: https://discuss.leetcode.com/topic/60437/java-solution-using-priorityqueue-and-linkedlist*/
30+
/**
31+
* Credit: https://discuss.leetcode.com/topic/60437/java-solution-using-priorityqueue-and-linkedlist
32+
*/
3133
public int[][] reconstructQueue(int[][] people) {
32-
Arrays.sort(people,new Comparator<int[]>(){
33-
public int compare(int[] p1, int[] p2){
34-
return p1[0] != p2[0] ? Integer.compare(p2[0],p1[0]) : Integer.compare(p1[1],p2[1]);
34+
Arrays.sort(people, new Comparator<int[]>() {
35+
public int compare(int[] p1, int[] p2) {
36+
return p1[0] != p2[0] ? Integer.compare(p2[0], p1[0]) : Integer.compare(p1[1], p2[1]);
3537
}
3638
});
3739
List<int[]> list = new LinkedList();
38-
for (int[] ppl: people) {
40+
for (int[] ppl : people) {
3941
list.add(ppl[1], ppl);
4042
}
4143
return list.toArray(new int[people.length][]);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public int splitArray(int[] nums, int m) {
6565
long l = max;
6666
long r = sum;
6767
while (l <= r) {
68-
long mid = (l + r)/ 2;
68+
long mid = (l + r) / 2;
6969
if (valid(mid, nums, m)) {
7070
r = mid - 1;
7171
} else {
@@ -78,7 +78,7 @@ public int splitArray(int[] nums, int m) {
7878
public boolean valid(long target, int[] nums, int m) {
7979
int count = 1;
8080
long total = 0;
81-
for(int num : nums) {
81+
for (int num : nums) {
8282
total += num;
8383
if (total > target) {
8484
total = num;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public class _412 {
3737

3838
public List<String> fizzBuzz(int n) {
3939
List<String> result = new ArrayList();
40-
for(int i = 1; i <= n; i++){
41-
if(i%3 == 0 && i%5 == 0){
40+
for (int i = 1; i <= n; i++) {
41+
if (i % 3 == 0 && i % 5 == 0) {
4242
result.add("_412");
43-
} else if(i%3 == 0){
43+
} else if (i % 3 == 0) {
4444
result.add("Fizz");
45-
} else if(i%5 == 0){
45+
} else if (i % 5 == 0) {
4646
result.add("Buzz");
4747
} else {
4848
result.add(Integer.toString(i));

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ public class _413 {
2929
//copied this solution: https://discuss.leetcode.com/topic/62884/2ms-java-o-n-time-o-1-space-solution
3030
public int numberOfArithmeticSlices(int[] A) {
3131
int sum = 0, len = 2;
32-
for (int i = 2; i < A.length; i++){
33-
if (A[i] - A[i-1] == A[i-1] - A[i-2]){
32+
for (int i = 2; i < A.length; i++) {
33+
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
3434
len++;
3535
} else {
3636
if (len > 2) sum += calculateSlices(len);
3737
len = 2;//reset it to 2
3838
}
3939
}
40-
if(len > 2) sum += calculateSlices(len);
40+
if (len > 2) sum += calculateSlices(len);
4141
return sum;
4242
}
43-
44-
int calculateSlices(int len){
45-
return (len-1)*(len-2)/2;
43+
44+
int calculateSlices(int len) {
45+
return (len - 1) * (len - 2) / 2;
4646
}
47-
48-
class Solution2{
47+
48+
class Solution2 {
4949
//a more clear solution: https://discuss.leetcode.com/topic/63302/simple-java-solution-9-lines-2ms
5050
public int numberOfArithmeticSlices(int[] A) {
5151
int sum = 0, curr = 0;
52-
for (int i = 2; i < A.length; i++){
53-
if (A[i] - A[i-1] == A[i-1] - A[i-2]){
52+
for (int i = 2; i < A.length; i++) {
53+
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
5454
curr++;
5555
sum += curr;
5656
} else {

0 commit comments

Comments
 (0)