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

+3-3
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

+1-1
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

+2-4
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

+1-4
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

+1-4
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

+1-5
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

+1-1
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

+1-1
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

+2-2
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

+16-23
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
}

src/main/java/io/github/spannm/leetcode/lc0/lc0800/Problem0863.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ List<Integer> distanceK(final TreeNode _root, final TreeNode _target, int _k) {
2222
for (int i = 0; i < size; i++) {
2323
TreeNode top = q.poll();
2424

25-
java.util.Optional.ofNullable(top.left).ifPresent(n -> {
25+
Optional.ofNullable(top.left).ifPresent(n -> {
2626
parent.put(n.val, top);
2727
q.offer(n);
2828
});
2929

30-
java.util.Optional.ofNullable(top.right).ifPresent(n -> {
30+
Optional.ofNullable(top.right).ifPresent(n -> {
3131
parent.put(n.val, top);
3232
q.offer(n);
3333
});

src/main/java/io/github/spannm/leetcode/lc0/lc0900/Problem0966.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ String[] spellchecker(String[] _wordlist, String[] _queries) {
1414
Set<String> set = new HashSet<>();
1515

1616
for (String word : _wordlist) {
17-
if (!caseMap.containsKey(word.toLowerCase())) {
18-
caseMap.put(word.toLowerCase(), word);
19-
}
17+
caseMap.putIfAbsent(word.toLowerCase(), word);
2018
set.add(word);
2119
}
2220

src/main/java/io/github/spannm/leetcode/lc1/lc1100/Problem1114.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,29 @@ private synchronized void doAction(int _order, Runnable _r) throws InterruptedEx
6767

6868
}
6969

70+
@SuppressWarnings("PMD.EmptyCatchBlock")
7071
public static void main(String[] _args) throws InterruptedException {
7172
Foo foo = new Foo(2, 1, 3);
7273
List<Thread> threads = new ArrayList<>(List.of(
7374
new Thread(() -> {
7475
try {
7576
foo.first(foo::first);
7677
} catch (InterruptedException _ex) {
77-
return;
7878
}
7979
}, "t1"),
8080
new Thread(() -> {
8181
try {
8282
foo.second(foo::second);
8383
} catch (InterruptedException _ex) {
84-
return;
8584
}
8685
}, "t2"),
8786
new Thread(() -> {
8887
try {
8988
foo.third(foo::third);
9089
} catch (InterruptedException _ex) {
91-
return;
9290
}
9391
}, "t3")));
9492
Collections.shuffle(threads);
95-
// threads.forEach(t -> t.setDaemon(false));
9693
threads.forEach(Thread::start);
9794

9895
Thread.sleep(500L);

src/main/java/io/github/spannm/leetcode/lc1/lc1100/Problem1115.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@ public void bar(Runnable _printBar) throws InterruptedException {
4545
}
4646
}
4747

48+
@SuppressWarnings("PMD.EmptyCatchBlock")
4849
public static void main(String[] _args) {
4950
FooBar fooBar = new FooBar(1);
5051
Thread ft = new Thread(() -> {
5152
try {
5253
fooBar.foo(() -> System.out.print("foo"));
5354
} catch (InterruptedException _ex) {
54-
return;
5555
}
5656
}, "fooThread");
5757
Thread bt = new Thread(() -> {
5858
try {
5959
fooBar.bar(() -> System.out.print("bar"));
6060
} catch (InterruptedException _ex) {
61-
return;
6261
}
6362
}, "barThread");
6463
ft.start();

src/main/java/io/github/spannm/leetcode/lc1/lc1100/Problem1117.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/**
99
* <a href="https://leetcode.com/problems/building-h2o/">1117. Building H2O</a>.
1010
*/
11+
@SuppressWarnings("PMD.EmptyCatchBlock")
1112
class Problem1117 extends LeetcodeProblem {
1213

1314
private final CyclicBarrier barrier = new CyclicBarrier(3);
@@ -21,7 +22,6 @@ public void hydrogen(Runnable _releaseHydrogen) {
2122
// releaseHydrogen.run() outputs "H". Do not change or remove this line.
2223
_releaseHydrogen.run();
2324
} catch (Exception _ex) {
24-
return;
2525
} finally {
2626
hSem.release();
2727
}
@@ -35,7 +35,6 @@ public void oxygen(Runnable _releaseOxygen) {
3535
// releaseOxygen.run() outputs "O". Do not change or remove this line.
3636
_releaseOxygen.run();
3737
} catch (Exception _ex) {
38-
return;
3938
} finally {
4039
oSem.release();
4140
}

src/main/java/io/github/spannm/leetcode/lc1/lc1200/Problem1258.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ private String formWord(String[] _words) {
4747
private Map<String, Set<String>> buildSynonymDict(String[] _words, List<List<String>> _synonyms) {
4848
Map<String, Set<String>> map = new HashMap<>();
4949
for (String key : _words) {
50-
if (!map.containsKey(key)) {
51-
map.put(key, new HashSet<>());
52-
}
53-
map.get(key).add(key);
50+
map.computeIfAbsent(key, k -> new HashSet<>()).add(key);
5451
}
5552
for (List<String> list : _synonyms) {
5653
for (String key : map.keySet()) {

src/main/java/io/github/spannm/leetcode/lc1/lc1200/Problem1286.java

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ static class CombinationIterator {
2424
private void buildAllCombinations(String _characters, int _start, StringBuilder _sb, boolean[] _visited) {
2525
if (_sb.length() == combinationLength) {
2626
list.add(_sb.toString());
27-
return;
2827
} else {
2928
for (int i = _start; i < _characters.length();) {
3029
if (!_visited[i]) {

src/main/java/io/github/spannm/leetcode/lc1/lc1300/Problem1344.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Problem1344 extends LeetcodeProblem {
77
double angleClock(int _hour, int _minutes) {
88
double minAngle = _minutes * 360 / 60;
99
double hourAnglePart1 = _hour != 12 ? _hour * 360 / 12 : 0;
10-
double hourAnglePart2 = (double) (30 * _minutes) / (double) 60;
10+
double hourAnglePart2 = (30 * _minutes) / 60D;
1111
double hourAngle = hourAnglePart1 + hourAnglePart2;
1212
double preResult = Math.abs(minAngle - hourAngle);
1313
return preResult > 180 ? 360 - preResult : preResult;

src/main/java/io/github/spannm/leetcode/lc1/lc1300/Problem1381.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
class Problem1381 extends LeetcodeProblem {
99

1010
static class CustomStack {
11-
1211
private final List<Integer> list;
1312
private final int maxSize;
1413

@@ -18,19 +17,13 @@ static class CustomStack {
1817
}
1918

2019
void push(int _x) {
21-
if (list.size() >= maxSize) {
22-
return;
23-
} else {
20+
if (list.size() < maxSize) {
2421
list.add(_x);
2522
}
2623
}
2724

2825
int pop() {
29-
if (!list.isEmpty()) {
30-
return list.remove(list.size() - 1);
31-
} else {
32-
return -1;
33-
}
26+
return list.isEmpty() ? -1 : list.remove(list.size() - 1);
3427
}
3528

3629
void increment(int _k, int _val) {

src/main/java/io/github/spannm/leetcode/lc1/lc1600/Problem1603.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ static class ParkingSystem2 {
3636
}
3737

3838
public boolean addCar(int _carType) {
39-
if (!slots.containsKey(_carType)) {
40-
return false;
41-
}
42-
return slots.compute(_carType, (k, v) -> --v) > -1;
39+
return slots.containsKey(_carType) && slots.compute(_carType, (k, v) -> --v) > -1;
4340
}
4441
}
4542

0 commit comments

Comments
 (0)