Skip to content

Commit 9a95106

Browse files
enable MultipleVariableDeclarations check!
1 parent f893b97 commit 9a95106

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+259
-115
lines changed

fishercoder_checkstyle.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
<property name="allowNoEmptyLineBetweenFields" value="true"/>
116116
</module>
117117

118-
<!--<module name="MultipleVariableDeclarations"/>-->
118+
<module name="MultipleVariableDeclarations"/>
119119

120120
<module name="SeparatorWrap">
121121
<property name="tokens" value="DOT"/>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public int maxProfit(int k, int[] prices) {
3030

3131

3232
private int quickSolve(int[] prices) {
33-
int len = prices.length, profit = 0;
33+
int len = prices.length;
34+
int profit = 0;
3435
for (int i = 1; i < len; i++) {
3536
// as long as there is a price gap, we gain a profit.
3637
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public ListNode removeNthFromEnd_one_pass(ListNode head, int n) {
5656
//then, when fast reaches null, slow reaches the point where the node should be deleted.
5757
ListNode dummy = new ListNode(-1);
5858
dummy.next = head;
59-
ListNode slow = head, fast = head;
59+
ListNode slow = head;
60+
ListNode fast = head;
6061
int tempN = n;
6162
while (tempN-- > 0) {
6263
fast = fast.next;
@@ -87,7 +88,8 @@ public ListNode removeNthFromEnd_one_pass(ListNode head, int n) {
8788
public ListNode removeNthFromEnd_one_pass_more_concise_version(ListNode head, int n) {
8889
ListNode dummy = new ListNode(-1);
8990
dummy.next = head;
90-
ListNode slow = dummy, fast = dummy;
91+
ListNode slow = dummy;
92+
ListNode fast = dummy;
9193
while (fast.next != null) {
9294
if (n <= 0) slow = slow.next;
9395
fast = fast.next;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ public static class DFSSolution {
3131
public int numIslands(char[][] grid) {
3232
if (grid == null || grid.length == 0) return 0;
3333
int count = 0;
34-
int m = grid.length, n = grid[0].length;
34+
int m = grid.length;
35+
int n = grid[0].length;
3536
for (int i = 0; i < m; i++) {
3637
for (int j = 0; j < n; j++) {
3738
if (grid[i][j] == '1') {
@@ -95,7 +96,8 @@ public int numIslands(char[][] grid) {
9596
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
9697
int[] dirs = new int[]{0, 1, 0, -1, 0};
9798
UnionFind uf = new UnionFind(grid);
98-
int m = grid.length, n = grid[0].length;
99+
int m = grid.length;
100+
int n = grid[0].length;
99101
for (int i = 0; i < m; i++) {
100102
for (int j = 0; j < n; j++) {
101103
if (grid[i][j] == '1') {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public class _203 {
2626
public ListNode removeElements(ListNode head, int val) {
2727
ListNode dummy = new ListNode(-1);
2828
dummy.next = head;
29-
ListNode curr = head, prev = dummy;
29+
ListNode curr = head;
30+
ListNode prev = dummy;
3031
while (curr != null) {
3132
if (curr.val == val) {
3233
prev.next = curr.next;

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public class _209 {
1515

1616
public int minSubArrayLen(int s, int[] nums) {
1717
if (nums == null || nums.length == 0) return 0;
18-
int i = 0, j = 0, min = Integer.MAX_VALUE, sum = 0;
18+
int i = 0;
19+
int j = 0;
20+
int min = Integer.MAX_VALUE;
21+
int sum = 0;
1922
while (j < nums.length) {
2023
sum += nums[j++];
2124

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class _221 {
1818
/**The idea is pretty straightforward: use a 2d dp table to store the intermediate results*/
1919
public static int maximalSquare(char[][] matrix) {
2020
if (matrix == null || matrix.length == 0) return 0;
21-
int m = matrix.length, n = matrix[0].length;
21+
int m = matrix.length;
22+
int n = matrix[0].length;
2223
int max = Integer.MIN_VALUE;
2324
int[][] dp = new int[m][n];
2425
for (int i = 0; i < m; i++) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class _233 {
1616
public int countDigitOne(int n) {
1717
int count = 0;
1818
for (long k = 1; k <= n; k *= 10) {
19-
long r = n / k, m = n % k;
19+
long r = n / k;
20+
long m = n % k;
2021
// sum up the count of ones on every place k
2122
count += (r + 8) / 10 * k + (r % 10 == 1 ? m + 1 : 0);
2223
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public static class Solution1 {
2121
public boolean isPalindrome(ListNode head) {
2222
if (head == null) return true;
2323

24-
ListNode slow = head, fast = head;
24+
ListNode slow = head;
25+
ListNode fast = head;
2526
while (fast.next != null && fast.next.next != null) {
2627
fast = fast.next.next;
2728
slow = slow.next;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ public class _243 {
1414

1515
public int shortestDistance(String[] words, String word1, String word2) {
1616

17-
int p = -1, q = -1, min = Integer.MAX_VALUE;
17+
int p = -1;
18+
int q = -1;
19+
int min = Integer.MAX_VALUE;
1820
for (int i = 0; i < words.length; i++) {
1921
if (words[i].equals(word1)) p = i;
2022
if (words[i].equals(word2)) q = i;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public int shortest(String word1, String word2) {
4242
List<Integer> list2 = map.get(word2);
4343
int ret = Integer.MAX_VALUE;
4444
for (int i = 0, j = 0; i < list1.size() && j < list2.size(); ) {
45-
int index1 = list1.get(i), index2 = list2.get(j);
45+
int index1 = list1.get(i);
46+
int index2 = list2.get(j);
4647
if (index1 < index2) {
4748
ret = Math.min(ret, index2 - index1);
4849
i++;

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
public class _246 {
1717

1818
public boolean isStrobogrammatic_map(String num) {
19-
int i = 0, j = num.length() - 1;
19+
int i = 0;
20+
int j = num.length() - 1;
2021
Map<Character, Character> map = new HashMap();
2122
map.put('8', '8');
2223
map.put('1', '1');
@@ -47,7 +48,8 @@ public boolean isStrobogrammatic_set(String num) {
4748
set.add('8');
4849
set.add('9');
4950
char[] nums = num.toCharArray();
50-
int i = 0, j = num.length() - 1;
51+
int i = 0;
52+
int j = num.length() - 1;
5153
while (i <= j) {
5254
if (!set.contains(nums[i]) || !set.contains(nums[j])) return false;
5355
if (nums[i] == '6' && nums[j] != '9') return false;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public int threeSumSmaller(int[] nums, int target) {
2323
int result = 0;
2424
Arrays.sort(nums);
2525
for (int i = 0; i < nums.length - 2; i++) {
26-
int left = i + 1, right = nums.length - 1;
26+
int left = i + 1;
27+
int right = nums.length - 1;
2728
while (left < right) {
2829
int sum = nums[i] + nums[left] + nums[right];
2930
if (sum < target) {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ public class _264 {
1515
public int nthUglyNumber(int n) {
1616
int[] ugly = new int[n];
1717
ugly[0] = 1;
18-
int index2 = 0, index3 = 0, index5 = 0;
19-
int factor2 = 2, factor3 = 3, factor5 = 5;
18+
int index2 = 0;
19+
int index3 = 0;
20+
int index5 = 0;
21+
int factor2 = 2;
22+
int factor3 = 3;
23+
int factor5 = 5;
2024
for (int i = 1; i < n; i++) {
2125
int min = Math.min(Math.min(factor2, factor3), factor5);
2226
ugly[i] = min;

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ public int minCostII(int[][] costs) {
1717
if (costs == null || costs.length == 0)
1818
return 0;
1919

20-
int n = costs.length, k = costs[0].length;
20+
int n = costs.length;
21+
int k = costs[0].length;
2122
// min1 is the index of the 1st-smallest cost till previous house
2223
// min2 is the index of the 2nd-smallest cost till previous house
23-
int min1 = -1, min2 = -1;
24+
int min1 = -1;
25+
int min2 = -1;
2426

2527
for (int i = 0; i < n; i++) {
26-
int last1 = min1, last2 = min2;
28+
int last1 = min1;
29+
int last2 = min2;
2730
min1 = -1;
2831
min2 = -1;
2932

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class _268 {
1313
/**we could take advantage of the array indices
1414
then a number xor with itself is zero, so after we xor the entire array with all of its indices, the missing number will show up.*/
1515
public int missingNumber(int[] nums) {
16-
int xor = 0, i = 0;
16+
int xor = 0;
17+
int i = 0;
1718
for (; i < nums.length; i++) {
1819
xor = xor ^ i ^ nums[i];
1920
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public int removeElement_editorial_solution_1(int[] nums, int val) {
2626
public int removeElement_editorial_solution_2(int[] nums, int val) {
2727
//this approach is very similar to the one below that I came up totally by myself, but it's much concise
2828
//Here, it didn't check whether nums[n-1] will be equal to val, because in the next iteration, it will still check that number, smart!
29-
int i = 0, n = nums.length;
29+
int i = 0;
30+
int n = nums.length;
3031
while (i < n) {
3132
if (nums[i] == val) {
3233
nums[i] = nums[n - 1];
@@ -41,7 +42,8 @@ public int removeElement_editorial_solution_2(int[] nums, int val) {
4142
//just throw all numbers that are equal to val to the end and make a count of it
4243
public int removeElement(int[] nums, int val) {
4344
int count = 0;
44-
int len = nums.length, throwPosition = len - 1;
45+
int len = nums.length;
46+
int throwPosition = len - 1;
4547
for (int i = 0; i <= throwPosition; i++) {
4648
while (throwPosition >= 0 && nums[throwPosition] == val) {
4749
throwPosition--;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class _276 {
1313
public int numWays(int n, int k) {
1414
if (n == 0) return 0;
1515
else if (n == 1) return k;
16-
int sameColorCnt = k, diffColorCnt = k * (k - 1);
16+
int sameColorCnt = k;
17+
int diffColorCnt = k * (k - 1);
1718
for (int i = 2; i < n; i++) {
1819
int temp = diffColorCnt;
1920
diffColorCnt = (diffColorCnt + sameColorCnt) * (k - 1);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ You are given an API bool isBadVersion(version) which will return whether versio
88
public class _278 {
99

1010
public int firstBadVersion(int n) {
11-
int left = 1, right = n;
11+
int left = 1;
12+
int right = n;
1213
if (isBadVersion(left))
1314
return left;
1415

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
public class _279 {
1111

1212
public int numSquares(int n) {
13-
int result = n, num = 2;
13+
int result = n;
14+
int num = 2;
1415
while (num * num <= n) {
15-
int temp1 = n / (num * num), temp2 = n % (num * num);
16+
int temp1 = n / (num * num);
17+
int temp2 = n % (num * num);
1618
result = Math.min(result, temp1 + numSquares(temp2));
1719
num++;
1820
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public void moveZeroes_Editorial_solution2(int[] nums) {
2626

2727
public void moveZeroes_Editorial_solution1(int[] nums) {
2828
//keep the last non-zero index and keep overwriting it, then append zeroes to fill the end
29-
int j = 0, i = 0;
29+
int j = 0;
30+
int i = 0;
3031
for (; j < nums.length; j++) {
3132
if (nums[j] != 0) {
3233
nums[i++] = nums[j];
@@ -59,7 +60,8 @@ public void moveZeroes(int[] nums) {
5960

6061
//this approach won't preserve the relative order of the non-zero numbers
6162
public void moveZeroes_1st_attempt(int[] nums) {
62-
int i = 0, j = nums.length - 1;
63+
int i = 0;
64+
int j = nums.length - 1;
6365
while (i < j) {
6466
if (nums[i] == 0) {
6567
int temp = nums[j];

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ class BFSSolutionWithoutQueue {
3131

3232
public void wallsAndGates(int[][] rooms) {
3333
if (rooms == null || rooms.length == 0 || rooms[0].length == 0) return;
34-
int m = rooms.length, n = rooms[0].length;
34+
int m = rooms.length;
35+
int n = rooms[0].length;
3536
for (int i = 0; i < m; i++) {
3637
for (int j = 0; j < n; j++) {
37-
if (rooms[i][j] == 0) bfs(rooms, i, j, m, n);
38+
if (rooms[i][j] == 0) {
39+
bfs(rooms, i, j, m, n);
40+
}
3841
}
3942
}
4043
}
@@ -59,7 +62,8 @@ class BFSSolutionWithAQueue {
5962

6063
public void wallsAndGates(int[][] rooms) {
6164
if (rooms == null || rooms.length == 0 || rooms[0].length == 0) return;
62-
int m = rooms.length, n = rooms[0].length;
65+
int m = rooms.length;
66+
int n = rooms[0].length;
6367
Queue<int[]> queue = new LinkedList();
6468
for (int i = 0; i < m; i++) {
6569
for (int j = 0; j < n; j++) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Write a function to compute the next state (after one update) of the board given
2525

2626
public class _289 {
2727
public void gameOfLife(int[][] board) {
28-
int height = board.length, width = board[0].length;
28+
int height = board.length;
29+
int width = board[0].length;
2930
int[][] next = new int[height][width];
3031

3132
for (int i = 0; i < board.length; i++) {

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Solution {
1919

2020
public int minArea(char[][] iImage, int x, int y) {
2121
image = iImage;
22-
int m = image.length, n = image[0].length;
22+
int m = image.length;
23+
int n = image[0].length;
2324
int left = searchColumns(0, y, 0, m, true);
2425
int right = searchColumns(y + 1, n, 0, m, false);
2526
int top = searchRows(0, x, left, right, true);
@@ -29,24 +30,30 @@ public int minArea(char[][] iImage, int x, int y) {
2930

3031
private int searchColumns(int i, int j, int top, int bottom, boolean opt) {
3132
while (i != j) {
32-
int k = top, mid = (i + j) / 2;
33-
while (k < bottom && image[k][mid] == '0') ++k;
34-
if (k < bottom == opt)
33+
int k = top;
34+
int mid = (i + j) / 2;
35+
while (k < bottom && image[k][mid] == '0') {
36+
++k;
37+
}
38+
if (k < bottom == opt) {
3539
j = mid;
36-
else
40+
} else {
3741
i = mid + 1;
42+
}
3843
}
3944
return i;
4045
}
4146

4247
private int searchRows(int i, int j, int left, int right, boolean opt) {
4348
while (i != j) {
44-
int k = left, mid = (i + j) / 2;
49+
int k = left;
50+
int mid = (i + j) / 2;
4551
while (k < right && image[mid][k] == '0') ++k;
46-
if (k < right == opt)
52+
if (k < right == opt) {
4753
j = mid;
48-
else
54+
} else {
4955
i = mid + 1;
56+
}
5057
}
5158
return i;
5259
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public int find(int[] father, int id) {
4646
int tf = father[id];
4747
while (tf != father[tf])
4848
tf = father[tf];
49-
int cur = id, tmp;
49+
int cur = id;
50+
int tmp;
5051
while (father[cur] != tf) {
5152
tmp = father[cur];
5253
father[cur] = tf;
@@ -79,7 +80,11 @@ public List<Integer> numIslands2(int m, int n, int[][] positions) {
7980
int[] sz = new int[m * n];
8081
int[] dr = { 0, 0, -1, 1 };
8182
int[] dc = { -1, 1, 0, 0 };
82-
int r, c, nr, nc, count = 0;
83+
int r;
84+
int c;
85+
int nr;
86+
int nc;
87+
int count = 0;
8388
for (int i = 0; i < positions.length; i++) {
8489
r = positions[i][0];
8590
c = positions[i][1];

0 commit comments

Comments
 (0)