Skip to content

Commit

Permalink
Implemenet findings from static code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Jun 22, 2024
1 parent efa9c34 commit 40402ac
Show file tree
Hide file tree
Showing 29 changed files with 115 additions and 136 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/github/spannm/leetcode/dep/Sudoku.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,17 @@ public boolean equals(Object _obj) {
return false;
}
Field other = (Field) _obj;
boolean equals = java.util.Objects.equals(value, other.value);
boolean equals = Objects.equals(value, other.value);
if (equals) {
equals = java.util.Objects.equals(areas.values().stream().map(Area::getItemNo).toList(),
equals = Objects.equals(areas.values().stream().map(Area::getItemNo).toList(),
other.areas.values().stream().map(Area::getItemNo).toList());
}
return equals;
}

@Override
public int hashCode() {
return java.util.Objects.hash(value, areas);
return Objects.hash(value, areas);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ List<List<Integer>> threeSum2(int[] _nums) {
}

public List<List<Integer>> threeSum(int[] _nums) {
List<Integer> input = java.util.Arrays.stream(_nums).sorted().boxed().toList();
List<Integer> input = Arrays.stream(_nums).sorted().boxed().toList();
final int len = input.size();
if (len < 3) {
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ boolean search(int[] _nums, int _target) {
break;
}
}
if (binSearch(_nums, 0, pivot, _target) >= 0) {
return true;
}
return binSearch(_nums, pivot + 1, len - 1, _target) > 0;
return binSearch(_nums, 0, pivot, _target) >= 0
|| binSearch(_nums, pivot + 1, len - 1, _target) > 0;
}

static int binSearch(int[] _nums, int _l, int _r, int _target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ static boolean search(TrieNode _node, String _word, int _index) {
return false;
} else {
TrieNode child = _node.children.get(c);
if (child == null) {
return false;
}
return search(child, _word, _index + 1);
return child != null && search(child, _word, _index + 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ int kthSmallest(TreeNode _root, int _k) {
return inorder.get(_k - 1);
}

private static void dfs(TreeNode _root, List<Integer> _list, int _k) {
static void dfs(TreeNode _root, List<Integer> _list, int _k) {
if (_root == null) {
return;
}
dfs(_root.left, _list, _k);
_list.add(_root.val);
dfs(_root.right, _list, _k);
if (_list.size() >= _k - 1) {
return;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ static boolean isMatch(String _str, int _i, String _pattern, int _j, Map<Charact

if (_map.containsKey(c)) {
String s = _map.get(c);

if (!_str.startsWith(s, _i)) {
return false;
}
return isMatch(_str, _i + s.length(), _pattern, _j + 1, _map, _set);
return _str.startsWith(s, _i) && isMatch(_str, _i + s.length(), _pattern, _j + 1, _map, _set);
}

for (int k = _i; k < _str.length(); k++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int largestPalindrome(int _n) {

int upperBound = (int) Math.pow(10, _n) - 1;
int lowerBound = upperBound / 10;
long maxNumber = (long) upperBound * (long) upperBound;
long maxNumber = upperBound * (long) upperBound;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void addNum(int _num) {
}

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

public void removeNum(int _num) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ static List<Integer> bfs(TreeNode _root, Map<Integer, Integer> _map) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode tn = queue.poll();
java.util.Optional.ofNullable(tn.left).ifPresent(queue::offer);
java.util.Optional.ofNullable(tn.right).ifPresent(queue::offer);
Optional.ofNullable(tn.left).ifPresent(queue::offer);
Optional.ofNullable(tn.right).ifPresent(queue::offer);
_map.compute(tn.val, (k, v) -> v == null ? 1 : v + 1);
}
}
Expand Down
39 changes: 16 additions & 23 deletions src/main/java/io/github/spannm/leetcode/lc0/lc0700/Problem0794.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,36 @@
*/
class Problem0794 extends LeetcodeProblem {

private String[] board;

public boolean validTicTacToe(String[] _board) {
board = _board;
int x = count('X');
int o = count('O');
if (x != o && x - 1 != o || win('X') && x - 1 != o) {
boolean validTicTacToe(String[] _board) {
int x = count(_board, 'X');
int o = count(_board, 'O');
if (x != o && x - 1 != o || win(_board, 'X') && x - 1 != o) {
return false;
}
return !(win('O') && x != o);
return !(win(_board, 'O') && x != o);
}

private boolean win(char _x) {
static boolean win(String[] _board, char _x) {
for (int i = 0; i < 3; i++) {
if (board[i].charAt(0) == _x && board[i].charAt(1) == _x && board[i].charAt(2) == _x) {
return true;
}
if (board[0].charAt(i) == _x && board[1].charAt(i) == _x && board[2].charAt(i) == _x) {
if ((_board[i].charAt(0) == _x && _board[i].charAt(1) == _x && _board[i].charAt(2) == _x)
|| (_board[0].charAt(i) == _x && _board[1].charAt(i) == _x && _board[2].charAt(i) == _x)) {
return true;
}
}
if (board[0].charAt(0) == _x && board[1].charAt(1) == _x && board[2].charAt(2) == _x) {
return true;
}
return board[0].charAt(2) == _x && board[1].charAt(1) == _x && board[2].charAt(0) == _x;
return (_board[0].charAt(0) == _x && _board[1].charAt(1) == _x && _board[2].charAt(2) == _x)
|| (_board[0].charAt(2) == _x && _board[1].charAt(1) == _x && _board[2].charAt(0) == _x);
}

private int count(char _x) {
int cnt = 0;
for (var row : board) {
for (var c : row.toCharArray()) {
static int count(String[] _board, char _x) {
int count = 0;
for (String row : _board) {
for (char c : row.toCharArray()) {
if (c == _x) {
cnt++;
count++;
}
}
}
return cnt;
return count;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ List<Integer> distanceK(final TreeNode _root, final TreeNode _target, int _k) {
for (int i = 0; i < size; i++) {
TreeNode top = q.poll();

java.util.Optional.ofNullable(top.left).ifPresent(n -> {
Optional.ofNullable(top.left).ifPresent(n -> {
parent.put(n.val, top);
q.offer(n);
});

java.util.Optional.ofNullable(top.right).ifPresent(n -> {
Optional.ofNullable(top.right).ifPresent(n -> {
parent.put(n.val, top);
q.offer(n);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ String[] spellchecker(String[] _wordlist, String[] _queries) {
Set<String> set = new HashSet<>();

for (String word : _wordlist) {
if (!caseMap.containsKey(word.toLowerCase())) {
caseMap.put(word.toLowerCase(), word);
}
caseMap.putIfAbsent(word.toLowerCase(), word);
set.add(word);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,29 @@ private synchronized void doAction(int _order, Runnable _r) throws InterruptedEx

}

@SuppressWarnings("PMD.EmptyCatchBlock")
public static void main(String[] _args) throws InterruptedException {
Foo foo = new Foo(2, 1, 3);
List<Thread> threads = new ArrayList<>(List.of(
new Thread(() -> {
try {
foo.first(foo::first);
} catch (InterruptedException _ex) {
return;
}
}, "t1"),
new Thread(() -> {
try {
foo.second(foo::second);
} catch (InterruptedException _ex) {
return;
}
}, "t2"),
new Thread(() -> {
try {
foo.third(foo::third);
} catch (InterruptedException _ex) {
return;
}
}, "t3")));
Collections.shuffle(threads);
// threads.forEach(t -> t.setDaemon(false));
threads.forEach(Thread::start);

Thread.sleep(500L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,19 @@ public void bar(Runnable _printBar) throws InterruptedException {
}
}

@SuppressWarnings("PMD.EmptyCatchBlock")
public static void main(String[] _args) {
FooBar fooBar = new FooBar(1);
Thread ft = new Thread(() -> {
try {
fooBar.foo(() -> System.out.print("foo"));
} catch (InterruptedException _ex) {
return;
}
}, "fooThread");
Thread bt = new Thread(() -> {
try {
fooBar.bar(() -> System.out.print("bar"));
} catch (InterruptedException _ex) {
return;
}
}, "barThread");
ft.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* <a href="https://leetcode.com/problems/building-h2o/">1117. Building H2O</a>.
*/
@SuppressWarnings("PMD.EmptyCatchBlock")
class Problem1117 extends LeetcodeProblem {

private final CyclicBarrier barrier = new CyclicBarrier(3);
Expand All @@ -21,7 +22,6 @@ public void hydrogen(Runnable _releaseHydrogen) {
// releaseHydrogen.run() outputs "H". Do not change or remove this line.
_releaseHydrogen.run();
} catch (Exception _ex) {
return;
} finally {
hSem.release();
}
Expand All @@ -35,7 +35,6 @@ public void oxygen(Runnable _releaseOxygen) {
// releaseOxygen.run() outputs "O". Do not change or remove this line.
_releaseOxygen.run();
} catch (Exception _ex) {
return;
} finally {
oSem.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ private String formWord(String[] _words) {
private Map<String, Set<String>> buildSynonymDict(String[] _words, List<List<String>> _synonyms) {
Map<String, Set<String>> map = new HashMap<>();
for (String key : _words) {
if (!map.containsKey(key)) {
map.put(key, new HashSet<>());
}
map.get(key).add(key);
map.computeIfAbsent(key, k -> new HashSet<>()).add(key);
}
for (List<String> list : _synonyms) {
for (String key : map.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static class CombinationIterator {
private void buildAllCombinations(String _characters, int _start, StringBuilder _sb, boolean[] _visited) {
if (_sb.length() == combinationLength) {
list.add(_sb.toString());
return;
} else {
for (int i = _start; i < _characters.length();) {
if (!_visited[i]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Problem1344 extends LeetcodeProblem {
double angleClock(int _hour, int _minutes) {
double minAngle = _minutes * 360 / 60;
double hourAnglePart1 = _hour != 12 ? _hour * 360 / 12 : 0;
double hourAnglePart2 = (double) (30 * _minutes) / (double) 60;
double hourAnglePart2 = (30 * _minutes) / 60D;
double hourAngle = hourAnglePart1 + hourAnglePart2;
double preResult = Math.abs(minAngle - hourAngle);
return preResult > 180 ? 360 - preResult : preResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class Problem1381 extends LeetcodeProblem {

static class CustomStack {

private final List<Integer> list;
private final int maxSize;

Expand All @@ -18,19 +17,13 @@ static class CustomStack {
}

void push(int _x) {
if (list.size() >= maxSize) {
return;
} else {
if (list.size() < maxSize) {
list.add(_x);
}
}

int pop() {
if (!list.isEmpty()) {
return list.remove(list.size() - 1);
} else {
return -1;
}
return list.isEmpty() ? -1 : list.remove(list.size() - 1);
}

void increment(int _k, int _val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ static class ParkingSystem2 {
}

public boolean addCar(int _carType) {
if (!slots.containsKey(_carType)) {
return false;
}
return slots.compute(_carType, (k, v) -> --v) > -1;
return slots.containsKey(_carType) && slots.compute(_carType, (k, v) -> --v) > -1;
}
}

Expand Down
Loading

0 comments on commit 40402ac

Please sign in to comment.