Skip to content

Commit 5f00a9f

Browse files
committed
Implement findings from static code analysis
1 parent efa9c34 commit 5f00a9f

29 files changed

+115
-136
lines changed

src/main/java/io/github/spannm/leetcode/dep/Sudoku.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,17 +467,17 @@ public boolean equals(Object _obj) {
467467
return false;
468468
}
469469
Field other = (Field) _obj;
470-
boolean equals = java.util.Objects.equals(value, other.value);
470+
boolean equals = Objects.equals(value, other.value);
471471
if (equals) {
472-
equals = java.util.Objects.equals(areas.values().stream().map(Area::getItemNo).toList(),
472+
equals = Objects.equals(areas.values().stream().map(Area::getItemNo).toList(),
473473
other.areas.values().stream().map(Area::getItemNo).toList());
474474
}
475475
return equals;
476476
}
477477

478478
@Override
479479
public int hashCode() {
480-
return java.util.Objects.hash(value, areas);
480+
return Objects.hash(value, areas);
481481
}
482482

483483
@Override

src/main/java/io/github/spannm/leetcode/lc0/lc0000/Problem0015.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ List<List<Integer>> threeSum2(int[] _nums) {
3939
}
4040

4141
public List<List<Integer>> threeSum(int[] _nums) {
42-
List<Integer> input = java.util.Arrays.stream(_nums).sorted().boxed().toList();
42+
List<Integer> input = Arrays.stream(_nums).sorted().boxed().toList();
4343
final int len = input.size();
4444
if (len < 3) {
4545
return List.of();

src/main/java/io/github/spannm/leetcode/lc0/lc0000/Problem0081.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ boolean search(int[] _nums, int _target) {
1616
break;
1717
}
1818
}
19-
if (binSearch(_nums, 0, pivot, _target) >= 0) {
20-
return true;
21-
}
22-
return binSearch(_nums, pivot + 1, len - 1, _target) > 0;
19+
return binSearch(_nums, 0, pivot, _target) >= 0
20+
|| binSearch(_nums, pivot + 1, len - 1, _target) > 0;
2321
}
2422

2523
static int binSearch(int[] _nums, int _l, int _r, int _target) {

src/main/java/io/github/spannm/leetcode/lc0/lc0200/Problem0211.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ static boolean search(TrieNode _node, String _word, int _index) {
4646
return false;
4747
} else {
4848
TrieNode child = _node.children.get(c);
49-
if (child == null) {
50-
return false;
51-
}
52-
return search(child, _word, _index + 1);
49+
return child != null && search(child, _word, _index + 1);
5350
}
5451
}
5552

src/main/java/io/github/spannm/leetcode/lc0/lc0200/Problem0230.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ int kthSmallest(TreeNode _root, int _k) {
1414
return inorder.get(_k - 1);
1515
}
1616

17-
private static void dfs(TreeNode _root, List<Integer> _list, int _k) {
17+
static void dfs(TreeNode _root, List<Integer> _list, int _k) {
1818
if (_root == null) {
1919
return;
2020
}
2121
dfs(_root.left, _list, _k);
2222
_list.add(_root.val);
2323
dfs(_root.right, _list, _k);
24-
if (_list.size() >= _k - 1) {
25-
return;
26-
}
2724
}
2825

2926
}

src/main/java/io/github/spannm/leetcode/lc0/lc0200/Problem0291.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ static boolean isMatch(String _str, int _i, String _pattern, int _j, Map<Charact
2424

2525
if (_map.containsKey(c)) {
2626
String s = _map.get(c);
27-
28-
if (!_str.startsWith(s, _i)) {
29-
return false;
30-
}
31-
return isMatch(_str, _i + s.length(), _pattern, _j + 1, _map, _set);
27+
return _str.startsWith(s, _i) && isMatch(_str, _i + s.length(), _pattern, _j + 1, _map, _set);
3228
}
3329

3430
for (int k = _i; k < _str.length(); k++) {

src/main/java/io/github/spannm/leetcode/lc0/lc0400/Problem0479.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int largestPalindrome(int _n) {
1111

1212
int upperBound = (int) Math.pow(10, _n) - 1;
1313
int lowerBound = upperBound / 10;
14-
long maxNumber = (long) upperBound * (long) upperBound;
14+
long maxNumber = upperBound * (long) upperBound;
1515

1616
int firstHalf = (int) (maxNumber / (long) Math.pow(10, _n));
1717

src/main/java/io/github/spannm/leetcode/lc0/lc0400/Problem0480.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void addNum(int _num) {
4949
}
5050

5151
public double findMedian() {
52-
return (k & 1) == 1 ? small.peek() : ((double) small.peek() + large.peek()) / 2;
52+
return (k & 1) == 1 ? small.peek() : (small.peek() + large.peek()) / 2D;
5353
}
5454

5555
public void removeNum(int _num) {

src/main/java/io/github/spannm/leetcode/lc0/lc0500/Problem0501.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ static List<Integer> bfs(TreeNode _root, Map<Integer, Integer> _map) {
2020
int size = queue.size();
2121
for (int i = 0; i < size; i++) {
2222
TreeNode tn = queue.poll();
23-
java.util.Optional.ofNullable(tn.left).ifPresent(queue::offer);
24-
java.util.Optional.ofNullable(tn.right).ifPresent(queue::offer);
23+
Optional.ofNullable(tn.left).ifPresent(queue::offer);
24+
Optional.ofNullable(tn.right).ifPresent(queue::offer);
2525
_map.compute(tn.val, (k, v) -> v == null ? 1 : v + 1);
2626
}
2727
}

src/main/java/io/github/spannm/leetcode/lc0/lc0700/Problem0794.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,36 @@
77
*/
88
class Problem0794 extends LeetcodeProblem {
99

10-
private String[] board;
11-
12-
public boolean validTicTacToe(String[] _board) {
13-
board = _board;
14-
int x = count('X');
15-
int o = count('O');
16-
if (x != o && x - 1 != o || win('X') && x - 1 != o) {
10+
boolean validTicTacToe(String[] _board) {
11+
int x = count(_board, 'X');
12+
int o = count(_board, 'O');
13+
if (x != o && x - 1 != o || win(_board, 'X') && x - 1 != o) {
1714
return false;
1815
}
19-
return !(win('O') && x != o);
16+
return !(win(_board, 'O') && x != o);
2017
}
2118

22-
private boolean win(char _x) {
19+
static boolean win(String[] _board, char _x) {
2320
for (int i = 0; i < 3; i++) {
24-
if (board[i].charAt(0) == _x && board[i].charAt(1) == _x && board[i].charAt(2) == _x) {
25-
return true;
26-
}
27-
if (board[0].charAt(i) == _x && board[1].charAt(i) == _x && board[2].charAt(i) == _x) {
21+
if ((_board[i].charAt(0) == _x && _board[i].charAt(1) == _x && _board[i].charAt(2) == _x)
22+
|| (_board[0].charAt(i) == _x && _board[1].charAt(i) == _x && _board[2].charAt(i) == _x)) {
2823
return true;
2924
}
3025
}
31-
if (board[0].charAt(0) == _x && board[1].charAt(1) == _x && board[2].charAt(2) == _x) {
32-
return true;
33-
}
34-
return board[0].charAt(2) == _x && board[1].charAt(1) == _x && board[2].charAt(0) == _x;
26+
return (_board[0].charAt(0) == _x && _board[1].charAt(1) == _x && _board[2].charAt(2) == _x)
27+
|| (_board[0].charAt(2) == _x && _board[1].charAt(1) == _x && _board[2].charAt(0) == _x);
3528
}
3629

37-
private int count(char _x) {
38-
int cnt = 0;
39-
for (var row : board) {
40-
for (var c : row.toCharArray()) {
30+
static int count(String[] _board, char _x) {
31+
int count = 0;
32+
for (String row : _board) {
33+
for (char c : row.toCharArray()) {
4134
if (c == _x) {
42-
cnt++;
35+
count++;
4336
}
4437
}
4538
}
46-
return cnt;
39+
return count;
4740
}
4841

4942
}

0 commit comments

Comments
 (0)