Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 077b107

Browse files
refactor for format
1 parent 0d11e57 commit 077b107

File tree

11 files changed

+86
-29
lines changed

11 files changed

+86
-29
lines changed

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,23 @@ public boolean canPlaceFlowers_more_concise_version(int[] flowerbed, int n) {
3333
count++;
3434
flowerbed[i] = 1;
3535
}
36-
if (count >= n) return true;
36+
if (count >= n) {
37+
return true;
38+
}
3739
i++;
3840
}
39-
if (count >= n) return true;
41+
if (count >= n) {
42+
return true;
43+
}
4044
return false;
4145
}
4246

4347
public boolean canPlaceFlowers(int[] flowerbed, int n) {
4448
int len = flowerbed.length;
4549
if (len == 1) {
46-
if ((flowerbed[0] == 0 && n <= 1) || n == 0) return true;
50+
if ((flowerbed[0] == 0 && n <= 1) || n == 0) {
51+
return true;
52+
}
4753
return false;
4854
}
4955
if (flowerbed[0] == 0 && flowerbed[1] == 0) {
@@ -55,10 +61,16 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
5561
n--;
5662
flowerbed[i] = 1;//modify the input, discuss this with interviwer, if not allowed, then have a copy of this input and modify copy
5763
}
58-
if (n <= 0) return true;
64+
if (n <= 0) {
65+
return true;
66+
}
67+
}
68+
if (len >= 2 && flowerbed[len-2] == 0 && flowerbed[len-1] == 0) {
69+
n--;
70+
}
71+
if (n <= 0) {
72+
return true;
5973
}
60-
if (len >= 2 && flowerbed[len-2] == 0 && flowerbed[len-1] == 0) n--;
61-
if (n <= 0) return true;
6274
return false;
6375
}
6476

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@
4040
public class _606 {
4141

4242
public String tree2str(TreeNode t) {
43-
if (t == null) return "";
43+
if (t == null) {
44+
return "";
45+
}
4446
String result = "" + t.val;
4547
String left = tree2str(t.left);
4648
String right = tree2str(t.right);
47-
if (left.equals("") && right.equals("")) return result;
48-
if (left.equals("")) return result + "()(" + right + ")";
49-
if (right.equals("")) return result + "(" + left + ")";
49+
if (left.equals("") && right.equals("")) {
50+
return result;
51+
}
52+
if (left.equals("")) {
53+
return result + "()(" + right + ")";
54+
}
55+
if (right.equals("")) {
56+
return result + "(" + left + ")";
57+
}
5058
return result + "(" + left + ")(" + right + ")";
5159
}
5260

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public class _61 {
1616
//credit: https://discuss.leetcode.com/topic/26364/clean-java-solution-with-brief-explanation
1717
//link the tail of the linked list to the head to form a circle, then count to find the pint and cut it
1818
public ListNode rotateRight(ListNode head, int k) {
19-
if (head == null) return head;
19+
if (head == null) {
20+
return head;
21+
}
2022
ListNode copyHead = head;
2123
int len = 1;
2224
while (copyHead.next != null) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public class _611 {
2727
* If so, then these three could form a valid triangle.*/
2828

2929
public int triangleNumber(int[] nums) {
30-
if (nums == null || nums.length == 0) return 0;
30+
if (nums == null || nums.length == 0) {
31+
return 0;
32+
}
3133
Arrays.sort(nums);
3234
int triplets = 0;
3335
for (int i = 2; i < nums.length; i++) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public String addBoldTag(String s, String[] dict) {
4747
continue;
4848
}
4949
int j = i;
50-
while (j < s.length() && shouldBold[j]) j++;
50+
while (j < s.length() && shouldBold[j]) {
51+
j++;
52+
}
5153
stringBuilder.append("<b>" + s.substring(i, j) + "</b>");
5254
i = j-1;
5355
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232
public class _617 {
3333

3434
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
35-
if (t1 == null) return t2;
36-
if (t2 == null) return t1;
35+
if (t1 == null) {
36+
return t2;
37+
}
38+
if (t2 == null) {
39+
return t1;
40+
}
3741
TreeNode mergedNode = null;
3842
if (t1 != null && t2 != null) {
3943
mergedNode = new TreeNode(t1.val + t2.val);

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@ public class _62 {
1616
* position in the first row and the first column: either from left or top.*/
1717
public int uniquePaths(int m, int n) {
1818
int[][] dp = new int[m][n];
19-
for(int i = 0; i < m; i++) dp[i][0] = 1;
20-
for(int i = 0; i < n; i++) dp[0][i] = 1;
19+
for(int i = 0; i < m; i++) {
20+
dp[i][0] = 1;
21+
}
22+
for(int i = 0; i < n; i++) {
23+
dp[0][i] = 1;
24+
}
2125

2226
for(int i = 1; i < m; i++){
2327
for(int j = 1; j < n; j++){
2428
int ways = 0;
25-
if(i-1 >= 0) ways += dp[i-1][j];
26-
if(j-1 >= 0) ways += dp[i][j-1];
29+
if(i-1 >= 0) {
30+
ways += dp[i-1][j];
31+
}
32+
if(j-1 >= 0) {
33+
ways += dp[i][j-1];
34+
}
2735
dp[i][j] = ways;
2836
}
2937
}
@@ -36,8 +44,11 @@ public int uniquePaths_merged_for_loop(int m, int n) {
3644
int[][] dp = new int[m][n];
3745
for(int i = 0; i < m; i++){
3846
for(int j = 0; j < n; j++){
39-
if(i == 0 || j == 0) dp[i][j] = 1;
40-
else dp[i][j] = dp[i-1][j] + dp[i][j-1];
47+
if(i == 0 || j == 0) {
48+
dp[i][j] = 1;
49+
} else {
50+
dp[i][j] = dp[i-1][j] + dp[i][j-1];
51+
}
4152
}
4253
}
4354
return dp[m-1][n-1];

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public int leastInterval(char[] tasks, int n) {
4949
}
5050
}
5151
times++;
52-
if (maxHeap.isEmpty() && temp.size() == 0) break;
52+
if (maxHeap.isEmpty() && temp.size() == 0) {
53+
break;
54+
}
5355
i++;
5456
}
5557
for (Task task : temp) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public TreeNode addOneRow(TreeNode root, int v, int d) {
8282
}
8383

8484
private void dfs(TreeNode root, int v, int d) {
85-
if (root == null) return;;
85+
if (root == null) {
86+
return;
87+
}
8688
if (d == 2) {
8789
TreeNode newLeft = new TreeNode(v);
8890
TreeNode newRight = new TreeNode(v);

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class _625 {
2828
* and https://leetcode.com/articles/minimum-factorization/#approach-3-using-factorizationaccepted*/
2929
public int smallestFactorization(int a) {
3030
//case 1: a < 10
31-
if (a < 10) return a;
31+
if (a < 10) {
32+
return a;
33+
}
3234

3335
//case 2: start with 9 and try every possible digit
3436
List<Integer> resultArray = new ArrayList<>();
@@ -41,13 +43,17 @@ public int smallestFactorization(int a) {
4143
}
4244

4345
//if a could not be broken in form of digits, return 0
44-
if (a != 0) return 0;
46+
if (a != 0) {
47+
return 0;
48+
}
4549

4650
//get the result from the result array in reverse order
4751
long result = 0;
4852
for (int i = resultArray.size()-1; i >=0; i--) {
4953
result = result*10 + resultArray.get(i);
50-
if (result > Integer.MAX_VALUE) return 0;
54+
if (result > Integer.MAX_VALUE) {
55+
return 0;
56+
}
5157
}
5258
return (int) result;
5359
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ public class _629 {
3030
* https://discuss.leetcode.com/topic/93815/java-dp-o-nk-solution*/
3131
public int kInversePairs(int n, int k) {
3232
int mod = 1000000007;
33-
if (k > n*(n-1)/2 || k < 0) return 0;
34-
if (k == 0 || k == n*(n-1)/2) return 1;
33+
if (k > n*(n-1)/2 || k < 0) {
34+
return 0;
35+
}
36+
if (k == 0 || k == n*(n-1)/2) {
37+
return 1;
38+
}
3539
long[][] dp = new long[n+1][k+1];
3640
dp[2][0] = 1;
3741
dp[2][1] = 1;
3842
for (int i = 3; i <= n; i++) {
3943
dp[i][0] = 1;
4044
for (int j = 1; j <= Math.min(k, i*(i-1)/2); j++) {
4145
dp[i][j] = dp[i][j-1] + dp[i-1][j];
42-
if (j >= i) dp[i][j] -= dp[i-1][j-i];
46+
if (j >= i) {
47+
dp[i][j] -= dp[i-1][j-i];
48+
}
4349
dp[i][j] = (dp[i][j]+mod) % mod;
4450
}
4551
}

0 commit comments

Comments
 (0)