Skip to content

Commit ebc73f3

Browse files
refactor format
1 parent 1469ab6 commit ebc73f3

File tree

15 files changed

+242
-236
lines changed

15 files changed

+242
-236
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ public boolean searchMatrix(int[][] matrix, int target) {
2727
if (matrix == null || matrix.length == 0
2828
|| matrix[0].length == 0
2929
|| matrix[0][0] > target
30-
|| matrix[matrix.length-1][matrix[0].length-1] < target) {
30+
|| matrix[matrix.length - 1][matrix[0].length - 1] < target) {
3131
return false;
3232
}
3333
int m = matrix.length;
3434
int n = matrix[0].length;
3535
int left = 0;
36-
int right = m * n - 1 ;
36+
int right = m * n - 1;
3737
while (left <= right) {
38-
int mid = left + (right-left)/2;
39-
int row = mid/n;
40-
int col = mid%n;
38+
int mid = left + (right - left) / 2;
39+
int row = mid / n;
40+
int col = mid % n;
4141
if (matrix[row][col] == target) {
4242
return true;
4343
} else if (matrix[row][col] > target) {
44-
right = mid-1;
44+
right = mid - 1;
4545
} else {
46-
left = mid+1;
46+
left = mid + 1;
4747
}
4848
}
4949
return false;

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@
1717
public class _75 {
1818

1919
public void sortColors(int[] nums) {
20-
int zero = 0, two = nums.length-1;
21-
for(int i = 0; i <= two;){
22-
if(nums[i] == 0 && i > zero) {
20+
int zero = 0, two = nums.length - 1;
21+
for (int i = 0; i <= two; ) {
22+
if (nums[i] == 0 && i > zero) {
2323
swap(nums, i, zero++);
24-
} else if(nums[i] == 2 && i < two) {
24+
} else if (nums[i] == 2 && i < two) {
2525
swap(nums, i, two--);
2626
} else {
2727
i++;
2828
}
2929
}
3030
}
31-
32-
void swap(int[] nums, int m, int n){
31+
32+
void swap(int[] nums, int m, int n) {
3333
int temp = nums[m];
3434
nums[m] = nums[n];
3535
nums[n] = temp;
3636
}
3737

3838

39-
public static void main(String...args){
39+
public static void main(String... args) {
4040
// int[] nums = new int[]{0,1,2,0,2,1};
4141
// int[] nums = new int[]{0};
4242
// int[] nums = new int[]{2};
43-
int[] nums = new int[]{2,2,1};
43+
int[] nums = new int[]{2, 2, 1};
4444
// int[] nums = new int[]{1,0};
4545
}
4646
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,36 @@ public class _76 {
1717

1818
public String minWindow(String s, String t) {
1919
int[] counts = new int[256];
20-
for(char c : t.toCharArray()){
20+
for (char c : t.toCharArray()) {
2121
counts[c]++;
2222
}
2323

2424
int start = 0, end = 0, minStart = 0, minLen = Integer.MAX_VALUE, counter = t.length();
25-
while(end < s.length()){
26-
if(counts[s.charAt(end)] > 0) {
25+
while (end < s.length()) {
26+
if (counts[s.charAt(end)] > 0) {
2727
counter--;
2828
}
2929

3030
counts[s.charAt(end)]--;
3131
end++;
3232

33-
while(counter == 0){
34-
if(end-start < minLen){
33+
while (counter == 0) {
34+
if (end - start < minLen) {
3535
minStart = start;
36-
minLen = end-start;
36+
minLen = end - start;
3737
}
3838
counts[s.charAt(start)]++;
39-
if(counts[s.charAt(start)] > 0) {
39+
if (counts[s.charAt(start)] > 0) {
4040
counter++;
4141
}
4242
start++;
4343
}
4444
}
4545

46-
if(minLen == Integer.MAX_VALUE) {
46+
if (minLen == Integer.MAX_VALUE) {
4747
return "";
4848
}
49-
return s.substring(minStart, minStart+minLen);
49+
return s.substring(minStart, minStart + minLen);
5050
}
5151

5252
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ public class _77 {
2121
public List<List<Integer>> combine(int n, int k) {
2222
List<List<Integer>> result = new ArrayList();
2323
int[] nums = new int[n];
24-
for(int i = 0; i < n; i++){
25-
nums[i] = i+1;
24+
for (int i = 0; i < n; i++) {
25+
nums[i] = i + 1;
2626
}
2727
backtracking(k, 0, nums, new ArrayList(), result);
2828
return result;
2929
}
30-
31-
void backtracking(int k, int start, int[] nums, List<Integer> temp, List<List<Integer>> result){
32-
if(temp.size() == k){
30+
31+
void backtracking(int k, int start, int[] nums, List<Integer> temp, List<List<Integer>> result) {
32+
if (temp.size() == k) {
3333
List<Integer> newTemp = new ArrayList(temp);
3434
result.add(newTemp);
35-
} else if(temp.size() < k){
36-
for(int i = start; i < nums.length; i++){
35+
} else if (temp.size() < k) {
36+
for (int i = start; i < nums.length; i++) {
3737
temp.add(nums[i]);
38-
backtracking(k, i+1, nums, temp, result);
39-
temp.remove(temp.size()-1);
38+
backtracking(k, i + 1, nums, temp, result);
39+
temp.remove(temp.size() - 1);
4040
}
4141
}
4242
}

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
4545
return false;
4646
}
4747
visited[i][j] = true;
48-
if (search(board, word, i+1, j, pos+1)
49-
|| search(board, word, i-1, j, pos+1)
50-
|| search(board, word, i, j+1, pos+1)
51-
|| search(board, word, i, j-1, pos+1)) {
48+
if (search(board, word, i + 1, j, pos + 1)
49+
|| search(board, word, i - 1, j, pos + 1)
50+
|| search(board, word, i, j + 1, pos + 1)
51+
|| search(board, word, i, j - 1, pos + 1)) {
5252
return true;
5353
}
5454

@@ -58,39 +58,39 @@ boolean search(char[][] board, String word, int i, int j, int pos) {
5858

5959
}
6060

61-
//I made it this time, completely by myself! Cheers! This let me completely understand backtracking!
61+
//I made it this time, completely by myself! Cheers! This let me completely understand backtracking!
6262
public boolean exist(char[][] board, String word) {
6363
int m = board.length, n = board[0].length;
64-
for(int i = 0; i < m; i++){
65-
for(int j = 0; j < n; j++){
64+
for (int i = 0; i < m; i++) {
65+
for (int j = 0; j < n; j++) {
6666
boolean[][] visited = new boolean[m][n];
67-
if(dfs(board, visited, i, j, word, 0)) {
67+
if (dfs(board, visited, i, j, word, 0)) {
6868
return true;
6969
}
7070
}
7171
}
7272
return false;
7373
}
74-
75-
final int[] dirs = new int[]{0,1,0,-1,0};
76-
77-
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index){
78-
if(index >= word.length() || word.charAt(index) != board[row][col]) {
74+
75+
final int[] dirs = new int[]{0, 1, 0, -1, 0};
76+
77+
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index) {
78+
if (index >= word.length() || word.charAt(index) != board[row][col]) {
7979
return false;
80-
} else if(index == word.length()-1 && word.charAt(index) == board[row][col]) {
80+
} else if (index == word.length() - 1 && word.charAt(index) == board[row][col]) {
8181
visited[row][col] = true;
8282
return true;
8383
}
8484
visited[row][col] = true;//set it to true for this case
8585
boolean result = false;
86-
for(int i = 0; i < 4; i++){
87-
int nextRow = row+dirs[i];
88-
int nextCol = col+dirs[i+1];
89-
if(nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
86+
for (int i = 0; i < 4; i++) {
87+
int nextRow = row + dirs[i];
88+
int nextCol = col + dirs[i + 1];
89+
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
9090
continue;
9191
}
92-
result = dfs(board, visited, nextRow, nextCol, word, index+1);
93-
if(result) {
92+
result = dfs(board, visited, nextRow, nextCol, word, index + 1);
93+
if (result) {
9494
return result;
9595
} else {
9696
visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
@@ -99,7 +99,7 @@ boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word,
9999
return result;
100100
}
101101

102-
public static void main(String...strings){
102+
public static void main(String... strings) {
103103
_79 test = new _79();
104104
// char[][] board = new char[][]{
105105
// {'A','B','C','E'},
@@ -109,16 +109,16 @@ public static void main(String...strings){
109109
// String word = "ABCCED";
110110
// String word = "SEE";
111111
// String word = "ABCD";
112-
112+
113113
// char[][] board = new char[][]{
114114
// {'a','a'},
115115
// };
116116
// String word = "aaa";
117-
117+
118118
char[][] board = new char[][]{
119-
{'A','B','C','E'},
120-
{'S','F','E','S'},
121-
{'A','D','E','E'},
119+
{'A', 'B', 'C', 'E'},
120+
{'S', 'F', 'E', 'S'},
121+
{'A', 'D', 'E', 'E'},
122122
};
123123
String word = "ABCEFSADEESE";
124124
System.out.println(test.exist(board, word));

0 commit comments

Comments
 (0)