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

Commit 65ff5ec

Browse files
refactor for format
1 parent 3e8fb6e commit 65ff5ec

File tree

6 files changed

+66
-24
lines changed

6 files changed

+66
-24
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,30 @@ public class _72 {
1414

1515
public int minDistance(String word1, String word2) {
1616
int m = word1.length(), n = word2.length();
17-
if(m == 0) return n;
18-
if(n == 0) return m;
17+
if(m == 0) {
18+
return n;
19+
}
20+
if(n == 0) {
21+
return m;
22+
}
1923

2024
char[] str1 = word1.toCharArray();
2125
char[] str2 = word2.toCharArray();
2226

2327
int[][] table = new int[m+1][n+1];
24-
for(int i = 0; i < m+1; i++) table[i][0] = i;
25-
for(int j = 0; j < n+1; j++) table[0][j] = j;
28+
for(int i = 0; i < m+1; i++) {
29+
table[i][0] = i;
30+
}
31+
for(int j = 0; j < n+1; j++) {
32+
table[0][j] = j;
33+
}
2634

2735
for(int i = 1; i < m+1; i++){
2836
for(int j = 1; j < n+1; j++){
2937
int cost = 0;
30-
if(str1[i-1] != str2[j-1]) cost = 1;
38+
if(str1[i-1] != str2[j-1]) {
39+
cost = 1;
40+
}
3141
table[i][j] = Math.min(Math.min(table[i-1][j]+1, table[i][j-1]+1), table[i-1][j-1]+cost);
3242
}
3343
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public static class Solution1 {
1919
* Space: O(m*n)
2020
*/
2121
public void setZeroes(int[][] matrix) {
22-
if (matrix == null || matrix.length == 0) return;
22+
if (matrix == null || matrix.length == 0) {
23+
return;
24+
}
2325
int height = matrix.length, width = matrix[0].length;
2426
boolean[][] zero = new boolean[height][width];
2527
for (int i = 0; i < height; i++) {
@@ -85,7 +87,9 @@ public static class Solution3 {
8587
* Space: O(1)
8688
*/
8789
public void setZeroes(int[][] matrix) {
88-
if (matrix == null || matrix.length == 0) return;
90+
if (matrix == null || matrix.length == 0) {
91+
return;
92+
}
8993
int m = matrix.length;
9094
int n = matrix[0].length;
9195
boolean firstRow = false;

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ public class _75 {
1919
public void sortColors(int[] nums) {
2020
int zero = 0, two = nums.length-1;
2121
for(int i = 0; i <= two;){
22-
if(nums[i] == 0 && i > zero) swap(nums, i, zero++);
23-
else if(nums[i] == 2 && i < two) swap(nums, i, two--);
24-
else i++;
22+
if(nums[i] == 0 && i > zero) {
23+
swap(nums, i, zero++);
24+
} else if(nums[i] == 2 && i < two) {
25+
swap(nums, i, two--);
26+
} else {
27+
i++;
28+
}
2529
}
2630
}
2731

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public String minWindow(String s, String t) {
2323

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

2830
counts[s.charAt(end)]--;
2931
end++;
@@ -34,12 +36,16 @@ public String minWindow(String s, String t) {
3436
minLen = end-start;
3537
}
3638
counts[s.charAt(start)]++;
37-
if(counts[s.charAt(start)] > 0) counter++;
39+
if(counts[s.charAt(start)] > 0) {
40+
counter++;
41+
}
3842
start++;
3943
}
4044
}
4145

42-
if(minLen == Integer.MAX_VALUE) return "";
46+
if(minLen == Integer.MAX_VALUE) {
47+
return "";
48+
}
4349
return s.substring(minStart, minStart+minLen);
4450
}
4551

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public static List<List<Integer>> subsets(int[] nums) {
3030
List<List<Integer>> result = new ArrayList();
3131
List<Integer> empty = new ArrayList();
3232
result.add(empty);
33-
if (nums == null) return result;
33+
if (nums == null) {
34+
return result;
35+
}
3436
for (int i = 0; i < nums.length; i++) {
3537
List<List<Integer>> temp = new ArrayList();
3638
//you'll have to create a new one here, otherwise, it'll throw ConcurrentModificationException.

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

+26-10
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,28 @@ public boolean exist(char[][] board, String word) {
2828
visited = new boolean[m][n];
2929
for (int i = 0; i < m; i++) {
3030
for (int j = 0; j < n; j++) {
31-
if (word.charAt(0) == board[i][j] && search(board, word, i, j, 0)) return true;
31+
if (word.charAt(0) == board[i][j] && search(board, word, i, j, 0)) {
32+
return true;
33+
}
3234
}
3335
}
3436
return false;
3537
}
3638

3739
boolean search(char[][] board, String word, int i, int j, int pos) {
38-
if (pos == word.length()) return true;
39-
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || word.charAt(pos) != board[i][j] || visited[i][j]) return false;
40+
if (pos == word.length()) {
41+
return true;
42+
}
43+
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || word.charAt(pos) != board[i][j] || visited[i][j]) {
44+
return false;
45+
}
4046
visited[i][j] = true;
4147
if (search(board, word, i+1, j, pos+1)
4248
|| search(board, word, i-1, j, pos+1)
4349
|| search(board, word, i, j+1, pos+1)
44-
|| search(board, word, i, j-1, pos+1)) return true;
50+
|| search(board, word, i, j-1, pos+1)) {
51+
return true;
52+
}
4553

4654
visited[i][j] = false;
4755
return false;
@@ -55,7 +63,9 @@ public boolean exist(char[][] board, String word) {
5563
for(int i = 0; i < m; i++){
5664
for(int j = 0; j < n; j++){
5765
boolean[][] visited = new boolean[m][n];
58-
if(dfs(board, visited, i, j, word, 0)) return true;
66+
if(dfs(board, visited, i, j, word, 0)) {
67+
return true;
68+
}
5969
}
6070
}
6171
return false;
@@ -64,8 +74,9 @@ public boolean exist(char[][] board, String word) {
6474
final int[] dirs = new int[]{0,1,0,-1,0};
6575

6676
boolean dfs(char[][] board, boolean[][] visited, int row, int col, String word, int index){
67-
if(index >= word.length() || word.charAt(index) != board[row][col]) return false;
68-
else if(index == word.length()-1 && word.charAt(index) == board[row][col]) {
77+
if(index >= word.length() || word.charAt(index) != board[row][col]) {
78+
return false;
79+
} else if(index == word.length()-1 && word.charAt(index) == board[row][col]) {
6980
visited[row][col] = true;
7081
return true;
7182
}
@@ -74,10 +85,15 @@ else if(index == word.length()-1 && word.charAt(index) == board[row][col]) {
7485
for(int i = 0; i < 4; i++){
7586
int nextRow = row+dirs[i];
7687
int nextCol = col+dirs[i+1];
77-
if(nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) continue;
88+
if(nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length || visited[nextRow][nextCol]) {
89+
continue;
90+
}
7891
result = dfs(board, visited, nextRow, nextCol, word, index+1);
79-
if(result) return result;
80-
else visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
92+
if(result) {
93+
return result;
94+
} else {
95+
visited[nextRow][nextCol] = false;//set it back to false if this road doesn't work to allow it for other paths, this is backtracking!!!
96+
}
8197
}
8298
return result;
8399
}

0 commit comments

Comments
 (0)