Skip to content

Commit 1a0cad4

Browse files
enable OneStatementPerLine check
1 parent f9b3976 commit 1a0cad4

15 files changed

+117
-64
lines changed

fishercoder_checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
<!--<message key="ws.notPreceded"-->
8686
<!--value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>-->
8787
<!--</module>-->
88-
<!--<module name="OneStatementPerLine"/>-->
88+
<module name="OneStatementPerLine"/>
8989
<!--<module name="MultipleVariableDeclarations"/>-->
9090
<module name="ArrayTypeStyle"/>
9191
<!--<module name="MissingSwitchDefault"/>-->

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@ public int numDistinct(String s, String t) {
1717
char[] schar = s.toCharArray();
1818
char[] tchar = t.toCharArray();
1919

20-
for(int i = 0; i <= m; i++) dp[i][0] = 1;
20+
for(int i = 0; i <= m; i++) {
21+
dp[i][0] = 1;
22+
}
2123

22-
for(int j = 1; j <= n; j++) dp[0][j] = 0;
24+
for(int j = 1; j <= n; j++) {
25+
dp[0][j] = 0;
26+
}
2327

2428
for(int i = 1; i <= m; i++){
2529
for(int j = 1; j <= n; j++){
26-
if(schar[i-1] == tchar[j-1]) dp[i][j] = dp[i-1][j]+dp[i-1][j-1];
27-
else dp[i][j] = dp[i-1][j];
30+
if(schar[i-1] == tchar[j-1]) {
31+
dp[i][j] = dp[i-1][j]+dp[i-1][j-1];
32+
} else {
33+
dp[i][j] = dp[i-1][j];
34+
}
2835
}
2936
}
3037

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313
public class _125 {
1414

1515
public boolean isPalindrome(String s) {
16-
int i = 0, j = s.length()-1;
16+
int i = 0, j = s.length() - 1;
1717
char[] chars = s.toCharArray();
18-
while(i < j){
19-
while(i < j && !Character.isLetterOrDigit(chars[i])) i++;
20-
while(i < j && !Character.isLetterOrDigit(chars[j])) j--;
21-
if(Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) return false;
22-
i++; j--;
18+
while (i < j) {
19+
while (i < j && !Character.isLetterOrDigit(chars[i])) {
20+
i++;
21+
}
22+
while (i < j && !Character.isLetterOrDigit(chars[j])) {
23+
j--;
24+
}
25+
if (Character.toLowerCase(chars[i]) != Character.toLowerCase(chars[j])) {
26+
return false;
27+
}
28+
i++;
29+
j--;
2330
}
2431
return true;
2532
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class UnionFind{
3333

3434
public UnionFind(int[] nums){
3535
ids = new int[nums.length];
36-
for(int i = 0; i < nums.length; i++) ids[i] = i;
36+
for(int i = 0; i < nums.length; i++) {
37+
ids[i] = i;
38+
}
3739
}
3840

3941
public void union(int i, int j){

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

+30-22
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,40 @@
55
import java.util.Map;
66
import java.util.Set;
77

8-
/**A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
9-
10-
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
11-
12-
For example, the numbers "69", "88", and "818" are all strobogrammatic.*/
8+
/**
9+
* A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
10+
* <p>
11+
* Write a function to determine if a number is strobogrammatic. The number is represented as a string.
12+
* <p>
13+
* For example, the numbers "69", "88", and "818" are all strobogrammatic.
14+
*/
1315

1416
public class _246 {
1517

1618
public boolean isStrobogrammatic_map(String num) {
17-
int i = 0, j = num.length()-1;
19+
int i = 0, j = num.length() - 1;
1820
Map<Character, Character> map = new HashMap();
1921
map.put('8', '8');
2022
map.put('1', '1');
2123
map.put('0', '0');
22-
if(j == 0) return map.containsKey(num.charAt(i));
23-
24+
if (j == 0) return map.containsKey(num.charAt(i));
25+
2426
map.put('9', '6');
2527
map.put('6', '9');
26-
while(i < j){
27-
if(!map.containsKey(num.charAt(i)) || !map.containsKey(num.charAt(j))) return false;
28-
if(map.get(num.charAt(i)) != num.charAt(j)) return false;
29-
i++;j--;
28+
while (i < j) {
29+
if (!map.containsKey(num.charAt(i)) || !map.containsKey(num.charAt(j))) {
30+
return false;
31+
}
32+
if (map.get(num.charAt(i)) != num.charAt(j)) {
33+
return false;
34+
}
35+
i++;
36+
j--;
3037
}
3138
return map.containsKey(num.charAt(i));
3239
}
3340

34-
41+
3542
public boolean isStrobogrammatic_set(String num) {
3643
Set<Character> set = new HashSet();
3744
set.add('0');
@@ -40,16 +47,17 @@ public boolean isStrobogrammatic_set(String num) {
4047
set.add('8');
4148
set.add('9');
4249
char[] nums = num.toCharArray();
43-
int i = 0, j = num.length()-1;
44-
while(i <= j){
45-
if(!set.contains(nums[i]) || !set.contains(nums[j])) return false;
46-
if(nums[i] == '6' && nums[j] != '9') return false;
47-
else if(nums[i] == '9' && nums[j] != '6') return false;
48-
else if(nums[i] == '1' && nums[j] != '1') return false;
49-
else if(nums[i] == '8' && nums[j] != '8') return false;
50-
else if(nums[i] == '0' && nums[j] != '0') return false;
50+
int i = 0, j = num.length() - 1;
51+
while (i <= j) {
52+
if (!set.contains(nums[i]) || !set.contains(nums[j])) return false;
53+
if (nums[i] == '6' && nums[j] != '9') return false;
54+
else if (nums[i] == '9' && nums[j] != '6') return false;
55+
else if (nums[i] == '1' && nums[j] != '1') return false;
56+
else if (nums[i] == '8' && nums[j] != '8') return false;
57+
else if (nums[i] == '0' && nums[j] != '0') return false;
5158
else {
52-
i++; j--;
59+
i++;
60+
j--;
5361
}
5462
}
5563
return true;

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ public class _261 {
2626

2727
public boolean validTree(int n, int[][] edges){
2828
int[] nums = new int[n];
29-
for(int i =0; i < n; i++) nums[i] = i;
29+
for(int i =0; i < n; i++) {
30+
nums[i] = i;
31+
}
3032

3133
for(int i = 0; i < edges.length; i++){
3234
int x = find(nums, edges[i][0]);
3335
int y = find(nums, edges[i][1]);
3436

35-
if(x == y) return false;
37+
if(x == y) {
38+
return false;
39+
}
3640

3741
//union
3842
nums[x] = y;
@@ -42,7 +46,9 @@ public boolean validTree(int n, int[][] edges){
4246
}
4347

4448
int find(int[] nums, int i){
45-
if(nums[i] == i) return i;
49+
if(nums[i] == i) {
50+
return i;
51+
}
4652
return find(nums, nums[i]);
4753
}
4854
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public _281(List<Integer> v1, List<Integer> v2) {
3535

3636
public int next() {
3737
if (j.hasNext()) {
38-
tmp = j; j = i; i = tmp;
38+
tmp = j;
39+
j = i;
40+
i = tmp;
3941
}
4042
return i.next();
4143
}

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ public String removeDuplicateLetters_use_stack(String s) {
5454
public String removeDuplicateLetters(String s) {
5555
int[] count = new int[26];
5656
int pos = 0; // the position for the smallest s[i]
57-
for (int i = 0; i < s.length(); i++) count[s.charAt(i) - 'a']++;
5857
for (int i = 0; i < s.length(); i++) {
59-
if (s.charAt(i) < s.charAt(pos)) pos = i;
58+
count[s.charAt(i) - 'a']++;
59+
}
60+
for (int i = 0; i < s.length(); i++) {
61+
if (s.charAt(i) < s.charAt(pos)) {
62+
pos = i;
63+
}
6064
count[s.charAt(i) - 'a']--;
61-
if (count[s.charAt(i) - 'a'] == 0) break;
65+
if (count[s.charAt(i) - 'a'] == 0) {
66+
break;
67+
}
6268
}
6369
return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters(s.substring(pos + 1).replaceAll("" + s.charAt(pos), ""));
6470
}

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,49 @@
1515
public class _34 {
1616

1717
public static int[] searchRange(int[] nums, int target) {
18-
int start = 0, end = nums.length-1;
18+
int start = 0, end = nums.length - 1;
1919
int[] range = new int[2];
20-
range[0] = -1; range[1] = -1;
21-
while(start+1 < end){
22-
int mid = start + (end-start)/2;
23-
if(nums[mid] == target) {
20+
range[0] = -1;
21+
range[1] = -1;
22+
while (start + 1 < end) {
23+
int mid = start + (end - start) / 2;
24+
if (nums[mid] == target) {
2425
int left = mid;
25-
while(left-1 >= 0 && nums[left] == nums[left-1]){
26+
while (left - 1 >= 0 && nums[left] == nums[left - 1]) {
2627
left--;
2728
}
2829
range[0] = left;
2930
int right = mid;
30-
while(right+1< nums.length && nums[right] == nums[right+1]){
31+
while (right + 1 < nums.length && nums[right] == nums[right + 1]) {
3132
right++;
3233
}
3334
range[1] = right;
3435
break;
35-
} else if(nums[mid] < target){
36+
} else if (nums[mid] < target) {
3637
start = mid;
3738
} else {
3839
end = mid;
3940
}
4041
}
41-
if(nums[start] == target){
42+
if (nums[start] == target) {
4243
range[0] = start;
43-
while(start+1 < nums.length && nums[start] == nums[start+1]){
44+
while (start + 1 < nums.length && nums[start] == nums[start + 1]) {
4445
start++;
4546
}
4647
range[1] = start;
4748
}
48-
if(nums[end] == target){
49+
if (nums[end] == target) {
4950
range[1] = end;
50-
while(end-1 >= 0 && nums[end] == nums[end-1]){
51+
while (end - 1 >= 0 && nums[end] == nums[end - 1]) {
5152
end--;
5253
}
5354
range[0] = end;
5455
}
5556
return range;
5657
}
5758

58-
public static void main(String...strings){
59-
int[] nums = new int[]{1,2,3};
59+
public static void main(String... strings) {
60+
int[] nums = new int[]{1, 2, 3};
6061
int target = 2;
6162
int[] result = searchRange(nums, target);
6263
CommonUtils.printArray(result);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public String reverseVowels(String s) {
4242
char temp = left;
4343
sb.setCharAt(i, right);
4444
sb.setCharAt(j, temp);
45-
i++; j--;
45+
i++;
46+
j--;
4647
}
4748
return sb.toString();
4849
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static int lengthLongestPath(String input) {
2020
if(period.equals(input.charAt(i))) {
2121
isFile = true;
2222
}
23-
i++; currStrLen++;
23+
i++;
24+
currStrLen++;
2425
}
2526
if(isFile) {
2627
longestLen = Math.max(longestLen, currDirLen+currStrLen);

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ public class _410 {
5252
Both of them mean that we should lower mid because we need to find the minimum one. This leads to r = mid - 1;*/
5353

5454
public int splitArray(int[] nums, int m) {
55-
int max = 0; long sum = 0;
55+
int max = 0;
56+
long sum = 0;
5657
for (int num : nums) {
5758
max = Math.max(num, max);
5859
sum += num;
5960
}
60-
if (m == 1) return (int) sum;
61+
if (m == 1) {
62+
return (int) sum;
63+
}
6164
//binary search
62-
long l = max; long r = sum;
65+
long l = max;
66+
long r = sum;
6367
while (l <= r) {
6468
long mid = (l + r)/ 2;
6569
if (valid(mid, nums, m)) {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ public class _484 {
4040
*/
4141
public int[] findPermutation(String s) {
4242
int[] result = new int[s.length()+1];
43-
for (int i = 0; i <= s.length(); i++) result[i] = i+1;
43+
for (int i = 0; i <= s.length(); i++) {
44+
result[i] = i+1;
45+
}
4446
for (int i = 0; i < s.length(); i++) {
4547
if (s.charAt(i) == 'D') {
4648
int left = i;
47-
while (i < s.length() && s.charAt(i) == 'D') i++;
49+
while (i < s.length() && s.charAt(i) == 'D') {
50+
i++;
51+
}
4852
reverse(result, left, i);
4953
}
5054
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
5858
heap.offer(new Point(ball[0], ball[1], 0, ""));
5959
int m = maze.length, n = maze[0].length;
6060
Point[][] points = new Point[m][n];
61-
for (int i = 0; i < m*n; i++) points[i/n][i%n] = new Point(i/n, i%n);//initialize the length array
61+
for (int i = 0; i < m*n; i++) {
62+
points[i/n][i%n] = new Point(i/n, i%n);//initialize the length array
63+
}
6264
String[] ds = new String[]{"u", "r", "d", "l"};
6365
while (!heap.isEmpty()) {
6466
Point curr = heap.poll();

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public int shortestDistance(int[][] maze, int[] start, int[] destination) {
6060
queue.offer(new Point(start[0], start[1], 0));
6161
int m = maze.length, n = maze[0].length;
6262
int[][] length = new int[m][n];
63-
for (int i = 0; i < m*n; i++) length[i/n][i%n] = Integer.MAX_VALUE;//initialize the length array
63+
for (int i = 0; i < m*n; i++) {
64+
length[i/n][i%n] = Integer.MAX_VALUE;//initialize the length array
65+
}
6466

6567
while (!queue.isEmpty()) {
6668
Point curr = queue.poll();

0 commit comments

Comments
 (0)