Skip to content

Commit

Permalink
Fix static code analysis findings
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 3, 2024
1 parent c163e4b commit 57e3da4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public List<String> fullJustify(String[] _words, final int _maxWidth) {
int availWidth = _maxWidth;
List<String> output = new ArrayList<>();

// printf("Input: %s%n", Arrays.stream(_words).map(w -> "\"" + w + "\"").collect(Collectors.joining(",")));

final int wordCount = _words.length;

for (int w = 0; w < wordCount; w++) {
Expand Down Expand Up @@ -64,14 +62,6 @@ public List<String> fullJustify(String[] _words, final int _maxWidth) {

}

// printf("%s%n", output.stream().map(l -> "\"" + l + "\"").collect(Collectors.joining("," +
// System.lineSeparator())));

_words = null;
lines = null;
currLine = null;
System.gc();

return output;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public boolean isInterleave(final String _s1, final String _s2, final String _s3
}
}

// Arrays.stream(dp).map(arr -> IntStream.range(0, arr.length).mapToObj(idx -> arr[idx] ? "1":
// "0").collect(Collectors.joining(","))).forEach(System.out::println);

boolean result = dp[0][0];
dp = null;
return result;
return dp[0][0];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@
public class Problem0704 extends LeetcodeProblem {

public int search(final int[] _nums, final int _target) {
// printf("Search %d in %s %n", _target, Arrays.toString(_nums));

int low = 0;
int high = _nums.length - 1;
int mid = 0;

while (low <= high) {
mid = low + high >>> 1;
// printf("low %d, high %d, mid %d %n", low, high, mid);
// printf("low %s, high %s, mid %s %n%n",
// Integer.toBinaryString(low), Integer.toBinaryString(high), Integer.toBinaryString(mid));

if (_nums[mid] < _target) {
low = mid + 1;
Expand Down
82 changes: 40 additions & 42 deletions src/main/java/io/github/spannm/leetcode/lc1/lc1100/Problem1114.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import io.github.spannm.leetcode.LeetcodeProblem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

/**
Expand All @@ -18,18 +15,20 @@ static class Foo {
private final Queue<Integer> orderQueue = new LinkedBlockingQueue<>();
private volatile Integer next;

Foo() {
this(1, 2, 3);
}

Foo(Integer... _order) {
if (_order == null || _order.length != 3) {
throw new IllegalArgumentException("Invalid order");
throw new IllegalArgumentException("Invalid order: " + Arrays.toString(_order));
}
orderQueue.addAll(List.of(_order));
System.err.println("Order is " + orderQueue);
next = getNext();
}

private Integer getNext() {
Integer n = orderQueue.poll();
System.err.println("Polled next: " + n);
return n;
}

Expand Down Expand Up @@ -61,47 +60,46 @@ private synchronized void doAction(int _order, Runnable _r) throws InterruptedEx
while (next != _order) {
wait();
}
System.err.println("Order: " + _order + ", next: " + next);
_r.run();
next = getNext();
notifyAll();
}

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

Thread.sleep(500L);

for (Thread thread : threads) {
thread.join();
}
}
}

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);

for (Thread thread : threads) {
thread.join();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ public static void main(String[] _args) {
try {
fooBar.foo(() -> System.out.print("foo"));
} catch (InterruptedException _ex) {
// TODO Auto-generated catch block
_ex.printStackTrace();
return;
}
}, "fooThread");
Thread bt = new Thread(() -> {
try {
fooBar.bar(() -> System.out.print("bar"));
} catch (InterruptedException _ex) {
// TODO Auto-generated catch block
_ex.printStackTrace();
return;
}
}, "barThread");
ft.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
public class Problem3020 extends LeetcodeProblem {

public int maximumLength(int[] _nums) {
// Map<Integer, Long> cnt = Arrays.stream(_nums).boxed().collect(Collectors.groupingBy(i -> i,
// Collectors.counting()));
Map<Long, Integer> cnt = new HashMap<>();
for (int x : _nums) {
cnt.merge((long) x, 1, Integer::sum);
Expand Down

0 comments on commit 57e3da4

Please sign in to comment.