Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

+4-4
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

+11-10
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

+2-1
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

+3-3
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

+1-1
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

+10-10
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

+7-5
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

+2-2
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

+4-4
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

+10-10
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 {

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,26 @@ public boolean canPartition(int[] nums) {
4444
sum /= 2;
4545

4646
int n = nums.length;
47-
boolean[][] dp = new boolean[n+1][sum+1];
47+
boolean[][] dp = new boolean[n + 1][sum + 1];
4848
for (int i = 0; i < dp.length; i++) {
4949
Arrays.fill(dp[i], false);
5050
}
5151

5252
dp[0][0] = true;
5353

54-
for (int i = 1; i < n+1; i++) {
54+
for (int i = 1; i < n + 1; i++) {
5555
dp[i][0] = true;
5656
}
5757

58-
for (int j = 1; j < sum+1; j++) {
58+
for (int j = 1; j < sum + 1; j++) {
5959
dp[0][j] = false;
6060
}
6161

62-
for (int i = 1; i < n+1; i++) {
63-
for (int j = 1; j < sum+1; j++) {
64-
dp[i][j] = dp[i-1][j];
65-
if (j >= nums[i-1]) {
66-
dp[i][j] = (dp[i][j] || dp[i-1][j-nums[i-1]]);
62+
for (int i = 1; i < n + 1; i++) {
63+
for (int j = 1; j < sum + 1; j++) {
64+
dp[i][j] = dp[i - 1][j];
65+
if (j >= nums[i - 1]) {
66+
dp[i][j] = (dp[i][j] || dp[i - 1][j - nums[i - 1]]);
6767
}
6868
}
6969
}

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

+33-31
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,51 @@
1818

1919
public class _417 {
2020
//looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean
21-
22-
/**One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS.*/
21+
22+
/**
23+
* One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS.
24+
*/
2325
public List<int[]> pacificAtlantic(int[][] matrix) {
2426

2527
List<int[]> result = new ArrayList();
26-
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return result;
27-
28+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return result;
29+
2830
int m = matrix.length, n = matrix[0].length;
2931
boolean[][] pacific = new boolean[m][n];
3032
boolean[][] atlantic = new boolean[m][n];
31-
32-
for(int i = 0; i < m; i++){
33+
34+
for (int i = 0; i < m; i++) {
3335
dfs(matrix, pacific, Integer.MIN_VALUE, i, 0);
34-
dfs(matrix, atlantic, Integer.MIN_VALUE, i, n-1);
36+
dfs(matrix, atlantic, Integer.MIN_VALUE, i, n - 1);
3537
}
36-
37-
for(int i = 0; i < n; i++){
38+
39+
for (int i = 0; i < n; i++) {
3840
dfs(matrix, pacific, Integer.MIN_VALUE, 0, i);
39-
dfs(matrix, atlantic, Integer.MIN_VALUE, m-1, i);
41+
dfs(matrix, atlantic, Integer.MIN_VALUE, m - 1, i);
4042
}
41-
42-
for(int i = 0; i < m; i++){
43-
for(int j = 0; j < n; j++){
44-
if(pacific[i][j] && atlantic[i][j]){
43+
44+
for (int i = 0; i < m; i++) {
45+
for (int j = 0; j < n; j++) {
46+
if (pacific[i][j] && atlantic[i][j]) {
4547
result.add(new int[]{i, j});
4648
}
4749
}
4850
}
49-
51+
5052
return result;
5153
}
52-
53-
void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y){
54+
55+
void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) {
5456
int m = matrix.length, n = matrix[0].length;
55-
if(x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) return;
57+
if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) return;
5658
visited[x][y] = true;
57-
dfs(matrix, visited, matrix[x][y], x+1, y);
58-
dfs(matrix, visited, matrix[x][y], x, y+1);
59-
dfs(matrix, visited, matrix[x][y], x-1, y);
60-
dfs(matrix, visited, matrix[x][y], x, y-1);
59+
dfs(matrix, visited, matrix[x][y], x + 1, y);
60+
dfs(matrix, visited, matrix[x][y], x, y + 1);
61+
dfs(matrix, visited, matrix[x][y], x - 1, y);
62+
dfs(matrix, visited, matrix[x][y], x, y - 1);
6163
}
62-
63-
public static void main(String...args){
64+
65+
public static void main(String... args) {
6466
_417 test = new _417();
6567
// int[][] matrix = new int[][]{
6668
// {1,2,2,3,5},
@@ -69,24 +71,24 @@ public static void main(String...args){
6971
// {6,7,1,4,5},
7072
// {5,1,1,2,4},
7173
// };
72-
74+
7375
// int[][] matrix = new int[][]{//this one is correct
7476
// {3,5},
7577
// {4,4},
7678
// };
77-
79+
7880
// int[][] matrix = new int[][]{//this one is correct
7981
// {2,3,5},
8082
// {3,4,4},
8183
// };
82-
84+
8385
int[][] matrix = new int[][]{
84-
{2,3,5},
85-
{3,4,4},
86-
{5,3,1},
86+
{2, 3, 5},
87+
{3, 4, 4},
88+
{5, 3, 1},
8789
};
8890
List<int[]> result = test.pacificAtlantic(matrix);
89-
for(int[] point : result){
91+
for (int[] point : result) {
9092
System.out.println(point[0] + "\t" + point[1]);
9193
}
9294
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public int wordsTyping(String[] sentence, int rows, int cols) {
7878
if (s.charAt(start % l) == ' ') {
7979
start++;
8080
} else {
81-
while (start > 0 && s.charAt((start-1) % l) != ' ') {
81+
while (start > 0 && s.charAt((start - 1) % l) != ' ') {
8282
start--;
8383
}
8484
}

0 commit comments

Comments
 (0)