Skip to content

Commit 686fb3e

Browse files
refactor format
1 parent faa26ff commit 686fb3e

File tree

15 files changed

+57
-51
lines changed

15 files changed

+57
-51
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@ public class _6 {
1717

1818
public String convert(String s, int numRows) {
1919
StringBuilder[] sb = new StringBuilder[numRows];
20-
char[] c= s.toCharArray();
20+
char[] c = s.toCharArray();
2121
int len = s.length();
22-
for(int i = 0; i < numRows; i++){
22+
for (int i = 0; i < numRows; i++) {
2323
sb[i] = new StringBuilder();//this is an important step to initialize it
2424
}
2525
int i = 0;
26-
while(i < len){
27-
for(int index = 0; index < numRows && i < len; index++){
26+
while (i < len) {
27+
for (int index = 0; index < numRows && i < len; index++) {
2828
sb[index].append(c[i++]);// vertically down
2929
}
30-
31-
for(int index = numRows - 2; index >= 1 && i < len; index--){/**Why it should start from numRows - 2? Think of the example when numRows = 3
32-
the starting point of obliquely going up is 1, which is numRows-2.*/
30+
31+
for (int index = numRows - 2; index >= 1 && i < len; index--) {
32+
/**Why it should start from numRows - 2? Think of the example when numRows = 3
33+
the starting point of obliquely going up is 1, which is numRows-2.*/
3334
sb[index].append(c[i++]);// obliquely up
3435
}
3536
}
36-
37-
for(i = 1; i < numRows; i++){
37+
38+
for (i = 1; i < numRows; i++) {
3839
sb[0].append(sb[i]);
3940
}
4041
return sb[0].toString();

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
public class _600 {
2424

2525
public static class DPSolution {
26-
/**Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted*/
26+
/**
27+
* Credit: https://leetcode.com/articles/non-negative-integers-without-consecutive-ones/#approach-3-using-bit-manipulation-accepted
28+
*/
2729
public int findIntegers(int num) {
2830
int[] f = new int[32];
2931
f[0] = 1;
@@ -49,7 +51,9 @@ public int findIntegers(int num) {
4951
}
5052
}
5153

52-
/**Brute force is definitely correct, but too time consuming and resulted in TLE.*/
54+
/**
55+
* Brute force is definitely correct, but too time consuming and resulted in TLE.
56+
*/
5357
public int findIntegers(int num) {
5458
int answer = 0;
5559
for (int i = 0; i <= num; i++) {
@@ -62,8 +66,8 @@ public int findIntegers(int num) {
6266

6367
private boolean hasConsecutiveOnes(int num) {
6468
String bin = Integer.toBinaryString(num);
65-
for (int i = 0; i < bin.length()-1; i++) {
66-
if (bin.charAt(i) == '1' && bin.charAt(i+1) == '1') {
69+
for (int i = 0; i < bin.length() - 1; i++) {
70+
if (bin.charAt(i) == '1' && bin.charAt(i + 1) == '1') {
6771
return false;
6872
}
6973
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public StringIterator(String compressedString) {
4848
while (j < len && Character.isDigit(compressedString.charAt(j))) {
4949
j++;
5050
}
51-
deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i+1, j))});
51+
deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i + 1, j))});
5252
i = j;
5353
}
5454
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public class _605 {
2828
public boolean canPlaceFlowers_more_concise_version(int[] flowerbed, int n) {
2929
int count = 0;
3030
int i = 0;
31-
while ( i < flowerbed.length) {
32-
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i-1] == 0) && (i == flowerbed.length-1 || flowerbed[i+1] == 0)) {
31+
while (i < flowerbed.length) {
32+
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
3333
count++;
3434
flowerbed[i] = 1;
3535
}
@@ -56,16 +56,16 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
5656
flowerbed[0] = 1;
5757
n--;
5858
}
59-
for (int i = 1; i < len-1; i++) {
60-
if (flowerbed[i] == 0 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0) {
59+
for (int i = 1; i < len - 1; i++) {
60+
if (flowerbed[i] == 0 && flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
6161
n--;
6262
flowerbed[i] = 1;//modify the input, discuss this with interviwer, if not allowed, then have a copy of this input and modify copy
6363
}
6464
if (n <= 0) {
6565
return true;
6666
}
6767
}
68-
if (len >= 2 && flowerbed[len-2] == 0 && flowerbed[len-1] == 0) {
68+
if (len >= 2 && flowerbed[len - 2] == 0 && flowerbed[len - 1] == 0) {
6969
n--;
7070
}
7171
if (n <= 0) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public List> findDuplicate(String[] paths) {
5757
for (String path : paths) {
5858
String[] dirAndFiles = path.split(" ");
5959
for (int i = 1; i < dirAndFiles.length; i++) {
60-
String content = dirAndFiles[i].substring(dirAndFiles[i].indexOf("(")+1,dirAndFiles[i].indexOf(")"));
60+
String content = dirAndFiles[i].substring(dirAndFiles[i].indexOf("(") + 1, dirAndFiles[i].indexOf(")"));
6161
if (!map.containsKey(content)) {
6262
map.put(content, new ArrayList<>());
6363
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ListNode rotateRight(ListNode head, int k) {
2626
len++;
2727
}
2828
copyHead.next = head;//link the tail and head to make it a circle
29-
for (int i = len - k%len; i > 1; i--) {
29+
for (int i = len - k % len; i > 1; i--) {
3030
head = head.next;
3131
}
3232
copyHead = head.next;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public int triangleNumber(int[] nums) {
3333
Arrays.sort(nums);
3434
int triplets = 0;
3535
for (int i = 2; i < nums.length; i++) {
36-
int left = 0, right = i-1;
36+
int left = 0, right = i - 1;
3737
while (left < right) {
3838
if (nums[i] < nums[left] + nums[right]) {
3939
triplets += (right - left);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public String addBoldTag(String s, String[] dict) {
5151
j++;
5252
}
5353
stringBuilder.append("" + s.substring(i, j) + "");
54-
i = j-1;
54+
i = j - 1;
5555
}
5656
return stringBuilder.toString();
5757
}

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,45 @@ 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++) {
19+
for (int i = 0; i < m; i++) {
2020
dp[i][0] = 1;
2121
}
22-
for(int i = 0; i < n; i++) {
22+
for (int i = 0; i < n; i++) {
2323
dp[0][i] = 1;
2424
}
25-
26-
for(int i = 1; i < m; i++){
27-
for(int j = 1; j < n; j++){
25+
26+
for (int i = 1; i < m; i++) {
27+
for (int j = 1; j < n; j++) {
2828
int ways = 0;
29-
if(i-1 >= 0) {
30-
ways += dp[i-1][j];
29+
if (i - 1 >= 0) {
30+
ways += dp[i - 1][j];
3131
}
32-
if(j-1 >= 0) {
33-
ways += dp[i][j-1];
32+
if (j - 1 >= 0) {
33+
ways += dp[i][j - 1];
3434
}
3535
dp[i][j] = ways;
3636
}
3737
}
3838
CommonUtils.printMatrix(dp);
39-
return dp[m-1][n-1];
39+
return dp[m - 1][n - 1];
4040
}
41-
41+
4242
//and we can actually put the two initialization for loop into the one
4343
public int uniquePaths_merged_for_loop(int m, int n) {
4444
int[][] dp = new int[m][n];
45-
for(int i = 0; i < m; i++){
46-
for(int j = 0; j < n; j++){
47-
if(i == 0 || j == 0) {
45+
for (int i = 0; i < m; i++) {
46+
for (int j = 0; j < n; j++) {
47+
if (i == 0 || j == 0) {
4848
dp[i][j] = 1;
4949
} else {
50-
dp[i][j] = dp[i-1][j] + dp[i][j-1];
50+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
5151
}
5252
}
5353
}
54-
return dp[m-1][n-1];
54+
return dp[m - 1][n - 1];
5555
}
56-
57-
public static void main(String...strings){
56+
57+
public static void main(String... strings) {
5858
_62 test = new _62();
5959
int m = 1;
6060
int n = 2;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public int leastInterval(char[] tasks, int n) {
4747
if (!maxHeap.isEmpty()) {
4848
if (maxHeap.peek().total > 1) {
4949
Task curr = maxHeap.poll();
50-
temp.add(new Task(curr.total-1, curr.character));
50+
temp.add(new Task(curr.total - 1, curr.character));
5151
} else {
5252
maxHeap.poll();
5353
}

0 commit comments

Comments
 (0)