Skip to content

Commit efed843

Browse files
refactor for format
1 parent 60e9885 commit efed843

34 files changed

+442
-421
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public int lengthOfLongestSubstring(String s) {
6666
}
6767

6868
public static class Solution3 {
69-
/**Sliding Window
69+
/**
70+
* Sliding Window
7071
* O(n) time
7172
* O(n) space
7273
*/
@@ -89,7 +90,8 @@ public int lengthOfLongestSubstring(String s) {
8990
}
9091

9192
public static class Solution4 {
92-
/**Sliding Window Optimized
93+
/**
94+
* Sliding Window Optimized
9395
* O(n) time
9496
* O(n) space
9597
*/
@@ -102,8 +104,8 @@ public int lengthOfLongestSubstring(String s) {
102104
/**Try to extend the range (i, j)*/
103105
for (int i = 0, j = 0; j < s.length(); j++) {
104106
i = Math.max(index[s.charAt(j)], i);
105-
max = Math.max(max, j -i + 1);
106-
index[s.charAt(j)] = j+1;
107+
max = Math.max(max, j - i + 1);
108+
index[s.charAt(j)] = j + 1;
107109
}
108110
return max;
109111
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,54 @@ public class _30 {
2121

2222
public List<Integer> findSubstring(String S, String[] L) {
2323
ArrayList<Integer> res = new ArrayList();
24-
if(S == null || L == null || S.length() == 0 || L.length == 0){
24+
if (S == null || L == null || S.length() == 0 || L.length == 0) {
2525
return res;
2626
}
2727

2828
HashMap<String, Integer> map = new HashMap();
29-
for(int i = 0; i < L.length; i++){
29+
for (int i = 0; i < L.length; i++) {
3030
Integer val = map.get(L[i]);
31-
if(val == null){
31+
if (val == null) {
3232
map.put(L[i], 1);
33-
} else{
33+
} else {
3434
map.put(L[i], val + 1);
3535
}
3636
}
3737
HashMap<String, Integer> original = new HashMap();
3838
original = (HashMap<String, Integer>) map.clone();
3939

4040
/* we use two start pointers, "start" means the real starting point,
41-
* "tempStart" means the currently starting point for comparing, if one whole concatenation substring
41+
* "tempStart" means the currently starting point for comparing, if one whole concatenation substring
4242
* is found, then we assign (tempStart + wordLen) to start, otherwise, start++. */
4343
int start = 0;
4444
int tempStart = 0;
4545
int wordLen = L[0].length();
4646
int wholeWordLen = wordLen * L.length;
47-
for(; start <= S.length() - wholeWordLen; start++){
47+
for (; start <= S.length() - wholeWordLen; start++) {
4848
map.clear();
4949
map = (HashMap<String, Integer>) original.clone();
50-
for(tempStart = start; tempStart < S.length(); tempStart += wordLen){
50+
for (tempStart = start; tempStart < S.length(); tempStart += wordLen) {
5151
/* assign start to tempStart, this is a very smart way of coding, learn and master it! */
52-
if(map.size() == 0){
52+
if (map.size() == 0) {
5353
break;
5454
}
55-
if( tempStart + wordLen > S.length()){
55+
if (tempStart + wordLen > S.length()) {
5656
break;
5757
}
5858
String sub = S.substring(tempStart, tempStart + wordLen);
5959
Integer val = map.get(sub);
6060

61-
if(val == null){
61+
if (val == null) {
6262
break;
63-
} else{
64-
if(val == 1){
63+
} else {
64+
if (val == 1) {
6565
map.remove(sub);
66-
} else{
66+
} else {
6767
map.put(sub, val - 1);
6868
}
6969
}
7070
}
71-
if(map.size() == 0){
71+
if (map.size() == 0) {
7272
res.add(start);
7373
}
7474
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public int lengthOfLIS(int[] nums) {
4444
* that this guarantees that the return value will be &gt;= 0 if
4545
* and only if the key is found.*/
4646
int index = Arrays.binarySearch(dp, 0, len, x);
47-
if (index < 0) index = -(index+1);
47+
if (index < 0) index = -(index + 1);
4848
dp[index] = x;
4949
if (index == len) len++;
5050
}
5151
return len;
5252
}
5353

54-
public static void main(String...args) {
54+
public static void main(String... args) {
5555
_300 test = new _300();
5656
int[] nums = new int[]{10, 9, 2, 5, 3, 7, 101, 18};
5757
System.out.println(test.lengthOfLIS(nums));

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class _301 {
2121

2222
public List<String> removeInvalidParentheses(String s) {
2323
List<String> result = new ArrayList<>();
24-
if(s == null) return result;
24+
if (s == null) return result;
2525

2626
Set<String> visited = new HashSet();
2727
Queue<String> q = new LinkedList();
@@ -31,20 +31,22 @@ public List<String> removeInvalidParentheses(String s) {
3131

3232
boolean found = false;
3333

34-
while(!q.isEmpty()){
34+
while (!q.isEmpty()) {
3535
String curr = q.poll();
36-
if(isValid(curr)){
36+
if (isValid(curr)) {
3737
found = true;
3838
result.add(curr);
3939
}
4040

41-
if(found) continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result
41+
if (found)
42+
continue;//this means if the initial input is already a valid one, we'll just directly return it and there's actually only one valid result
4243

43-
for(int i = 0; i < curr.length(); i++){
44-
if(curr.charAt(i) != '(' && curr.charAt(i) != ')') continue;//this is to rule out those non-parentheses characters
44+
for (int i = 0; i < curr.length(); i++) {
45+
if (curr.charAt(i) != '(' && curr.charAt(i) != ')')
46+
continue;//this is to rule out those non-parentheses characters
4547

46-
String next = curr.substring(0, i) + curr.substring(i+1);
47-
if(!visited.contains(next)){
48+
String next = curr.substring(0, i) + curr.substring(i + 1);
49+
if (!visited.contains(next)) {
4850
q.offer(next);
4951
visited.add(next);
5052
}
@@ -57,12 +59,12 @@ public List<String> removeInvalidParentheses(String s) {
5759
private boolean isValid(String str) {
5860
char[] chars = str.toCharArray();
5961
int count = 0;
60-
for(int i = 0; i < chars.length; i++){
62+
for (int i = 0; i < chars.length; i++) {
6163
char c = chars[i];
62-
if(c == '(') count++;
63-
if (c == ')'){
64+
if (c == '(') count++;
65+
if (c == ')') {
6466
count--;
65-
if(count == -1) return false;
67+
if (count == -1) return false;
6668
}
6769
}
6870
return count == 0;

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,38 @@ public class NumMatrix {
3232
int width;
3333

3434
public NumMatrix(int[][] matrix) {
35-
if(matrix.length == 0 || matrix[0].length == 0) return;
35+
if (matrix.length == 0 || matrix[0].length == 0) return;
3636
height = matrix.length;
3737
width = matrix[0].length;
3838
this.nums = new int[height][width];
39-
this.tree = new int[height+1][width+1];
40-
for(int i = 0; i < height; i++){
41-
for(int j = 0; j < width; j++){
39+
this.tree = new int[height + 1][width + 1];
40+
for (int i = 0; i < height; i++) {
41+
for (int j = 0; j < width; j++) {
4242
update(i, j, matrix[i][j]);
4343
}
4444
}
4545
}
4646

4747
public void update(int rowIndex, int colIndex, int newVal) {
48-
if(height == 0 || width == 0) return;
48+
if (height == 0 || width == 0) return;
4949
int delta = newVal - nums[rowIndex][colIndex];
5050
nums[rowIndex][colIndex] = newVal;
51-
for(int i = rowIndex+1; i <= height; i += i&(-i)){
52-
for(int j = colIndex+1; j <= width; j += j&(-j)){
51+
for (int i = rowIndex + 1; i <= height; i += i & (-i)) {
52+
for (int j = colIndex + 1; j <= width; j += j & (-j)) {
5353
tree[i][j] += delta;//just use its previous value plus delta is good
5454
}
5555
}
5656
}
5757

5858
public int sumRegion(int row1, int col1, int row2, int col2) {
59-
if(height == 0 || width == 0) return 0;
60-
return sum(row2+1, col2+1) + sum(row1, col1) - sum(row1, col2+1) - sum(row2+1, col1);
59+
if (height == 0 || width == 0) return 0;
60+
return sum(row2 + 1, col2 + 1) + sum(row1, col1) - sum(row1, col2 + 1) - sum(row2 + 1, col1);
6161
}
6262

6363
private int sum(int row, int col) {
6464
int sum = 0;
65-
for(int i = row; i > 0; i -= i&(-i)){
66-
for(int j = col; j > 0; j -= j&(-j)){
65+
for (int i = row; i > 0; i -= i & (-i)) {
66+
for (int j = col; j > 0; j -= j & (-j)) {
6767
sum += tree[i][j];
6868
}
6969
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,34 @@ public class _31 {
1818
* 2. if not, start from the end of the array, find the first pair of numbers that break the decrementing order
1919
* 3. then from that index going to the right again, find the element that is closest bigger than this number, swap them
2020
* 4. reverse the right half of this array after this index*/
21-
21+
2222
public void nextPermutation(int[] nums) {
23-
int i = nums.length-2;
24-
while (i >= 0 && nums[i] >= nums[i+1]) {
23+
int i = nums.length - 2;
24+
while (i >= 0 && nums[i] >= nums[i + 1]) {
2525
i--;
2626
}
2727
if (i >= 0) {
28-
int j = nums.length-1;
28+
int j = nums.length - 1;
2929
while (j >= 0 && nums[i] >= nums[j]) {
3030
j--;
3131
}
32-
32+
3333
swap(nums, i, j);
3434
}
35-
36-
reverse(nums, i+1);
35+
36+
reverse(nums, i + 1);
3737
}
38-
38+
3939
private void reverse(int[] nums, int start) {
40-
int end = nums.length-1;
41-
while (start <= end){
40+
int end = nums.length - 1;
41+
while (start <= end) {
4242
int tmp = nums[start];
4343
nums[start++] = nums[end];
4444
nums[end--] = tmp;
4545
}
4646
}
4747

48-
private void swap(int[] nums, int i, int j){
48+
private void swap(int[] nums, int i, int j) {
4949
int tmp = nums[i];
5050
nums[i] = nums[j];
5151
nums[j] = tmp;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public class _311 {
2626
public int[][] multiply(int[][] A, int[][] B) {
2727
int m = A.length, n = A[0].length, p = B[0].length;
2828
int[][] C = new int[m][p];
29-
for(int i = 0; i < m; i++){
30-
for(int j = 0; j < n; j++){
31-
if(A[i][j] != 0){
32-
for(int k = 0; k < p; k++){
33-
if(B[j][k] != 0) C[i][k] += A[i][j]*B[j][k];
29+
for (int i = 0; i < m; i++) {
30+
for (int j = 0; j < n; j++) {
31+
if (A[i][j] != 0) {
32+
for (int k = 0; k < p; k++) {
33+
if (B[j][k] != 0) C[i][k] += A[i][j] * B[j][k];
3434
}
3535
}
3636
}

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,74 +69,74 @@
6969
public class _314 {
7070
public List<List<Integer>> verticalOrder_using_treemap(TreeNode root) {
7171
List<List<Integer>> result = new ArrayList();
72-
if(root == null) return result;
72+
if (root == null) return result;
7373
Queue<TreeNode> bfsQ = new LinkedList();
7474
Queue<Integer> indexQ = new LinkedList();
7575
TreeMap<Integer, List<Integer>> map = new TreeMap();
7676
bfsQ.offer(root);
7777
indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive
78-
while(!bfsQ.isEmpty()){
78+
while (!bfsQ.isEmpty()) {
7979
int qSize = bfsQ.size();
80-
for(int i = 0; i < qSize; i++){
80+
for (int i = 0; i < qSize; i++) {
8181
TreeNode curr = bfsQ.poll();
8282
int index = indexQ.poll();
83-
if(map.containsKey(index)){
83+
if (map.containsKey(index)) {
8484
map.get(index).add(curr.val);
85-
} else if(!map.containsKey(index)){
85+
} else if (!map.containsKey(index)) {
8686
List<Integer> list = new ArrayList();
8787
list.add(curr.val);
8888
map.put(index, list);
8989
}
90-
if(curr.left != null){
90+
if (curr.left != null) {
9191
bfsQ.offer(curr.left);
92-
indexQ.offer(index-1);
92+
indexQ.offer(index - 1);
9393
}
94-
if(curr.right != null){
94+
if (curr.right != null) {
9595
bfsQ.offer(curr.right);
96-
indexQ.offer(index+1);
96+
indexQ.offer(index + 1);
9797
}
9898
}
9999
}
100-
for(int i : map.keySet()){
100+
for (int i : map.keySet()) {
101101
result.add(map.get(i));
102102
}
103103
return result;
104104
}
105105

106106
public List<List<Integer>> verticalOrder_using_hashmap(TreeNode root) {
107107
List<List<Integer>> result = new ArrayList();
108-
if(root == null) return result;
108+
if (root == null) return result;
109109
Queue<TreeNode> bfsQ = new LinkedList();
110110
Queue<Integer> indexQ = new LinkedList();
111111
HashMap<Integer, List<Integer>> map = new HashMap();
112112
bfsQ.offer(root);
113113
indexQ.offer(0);//we set the root as index 0, left will be negative, right will be positive
114114
int min = 0, max = 0;
115-
while(!bfsQ.isEmpty()){
115+
while (!bfsQ.isEmpty()) {
116116
int qSize = bfsQ.size();
117-
for(int i = 0; i < qSize; i++){
117+
for (int i = 0; i < qSize; i++) {
118118
TreeNode curr = bfsQ.poll();
119119
int index = indexQ.poll();
120-
if(map.containsKey(index)){
120+
if (map.containsKey(index)) {
121121
map.get(index).add(curr.val);
122-
} else if(!map.containsKey(index)){
122+
} else if (!map.containsKey(index)) {
123123
List<Integer> list = new ArrayList();
124124
list.add(curr.val);
125125
map.put(index, list);
126126
}
127-
if(curr.left != null){
127+
if (curr.left != null) {
128128
bfsQ.offer(curr.left);
129-
indexQ.offer(index-1);
130-
min = Math.min(min, index-1);
129+
indexQ.offer(index - 1);
130+
min = Math.min(min, index - 1);
131131
}
132-
if(curr.right != null){
132+
if (curr.right != null) {
133133
bfsQ.offer(curr.right);
134-
indexQ.offer(index+1);
135-
max = Math.max(max, index+1);
134+
indexQ.offer(index + 1);
135+
max = Math.max(max, index + 1);
136136
}
137137
}
138138
}
139-
for(int i = min; i <= max; i++){
139+
for (int i = min; i <= max; i++) {
140140
result.add(map.get(i));
141141
}
142142
return result;

0 commit comments

Comments
 (0)