Skip to content

Commit

Permalink
Fixed idea warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Nov 24, 2024
1 parent 113ad95 commit 818ed59
Show file tree
Hide file tree
Showing 27 changed files with 70 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class Solution {
public List<String> addOperators(String num, int target) {
List<String> res = new ArrayList<>();
if (num.length() == 0 || Long.valueOf(num) > Integer.MAX_VALUE) {
if (num.length() == 0 || Long.parseLong(num) > Integer.MAX_VALUE) {
return res;
}
char[] list = num.toCharArray();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0301_0400/s0400_nth_digit/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public int findNthDigit(int n) {
long count = 9;
int start = 1;
while (n > len * count) {
n -= len * count;
n -= (int) (len * count);
len += 1;
count *= 10;
start *= 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public int countBattleships(char[][] board) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] != '.'
&& (j <= 0 || board[i][j - 1] != 'X')
&& (j == 0 || board[i][j - 1] != 'X')
&& (i <= 0 || board[i - 1][j] != 'X')) {
count++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public String decodeAtIndex(String s, int k) {
}
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
k %= length;
k %= (int) length;
if (c >= 48 && c <= 57) {
length /= c - '0';
} else if (k == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public int bitwiseComplement(int n) {
int exp = list.size() - 1;
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) == 0) {
result += Math.pow(2, exp);
result += (int) Math.pow(2, exp);
}
exp--;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
package g1101_1200.s1195_fizz_buzz_multithreaded;

// #Medium #Concurrency #2022_03_03_Time_8_ms_(80.09%)_Space_43.2_MB_(6.17%)
// #Medium #Concurrency #2024_11_24_Time_6_ms_(94.88%)_Space_43.1_MB_(8.61%)

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntConsumer;

@SuppressWarnings("java:S1130")
public class FizzBuzz {
private final AtomicInteger count = new AtomicInteger(1);

private final int n;
private int current;

public FizzBuzz(int n) {
this.n = n;
this.current = 1;
}

// printFizz.run() outputs "fizz".
public void fizz(Runnable printFizz) throws InterruptedException {
int i;
while ((i = count.get()) <= n) {
if (i % 3 == 0 && i % 5 != 0) {
printFizz.run();
count.compareAndSet(i, i + 1);
synchronized (this) {
while (current <= n) {
if (current % 3 == 0 && current % 5 != 0) {
printFizz.run();
current += 1;
notifyAll();
} else {
wait();
}
}
}
}

// printBuzz.run() outputs "buzz".
public void buzz(Runnable printBuzz) throws InterruptedException {
int i;
while ((i = count.get()) <= n) {
count.get();
if (i % 5 == 0 && i % 3 != 0) {
printBuzz.run();
count.compareAndSet(i, i + 1);
synchronized (this) {
while (current <= n) {
if (current % 3 != 0 && current % 5 == 0) {
printBuzz.run();
current += 1;
notifyAll();
} else {
wait();
}
}
}
}

// printFizzBuzz.run() outputs "fizzbuzz".
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
int i;
while ((i = count.get()) <= n) {
if (i % 15 == 0) {
printFizzBuzz.run();
count.compareAndSet(i, i + 1);
synchronized (this) {
while (current <= n) {
if (current % 15 == 0) {
printFizzBuzz.run();
current += 1;
notifyAll();
} else {
wait();
}
}
}
}

// printNumber.accept(x) outputs "x", where x is an integer.
public void number(IntConsumer printNumber) throws InterruptedException {
int i;
while ((i = count.get()) <= n) {
if (i % 5 != 0 && i % 3 != 0) {
printNumber.accept(i);
count.compareAndSet(i, i + 1);
synchronized (this) {
while (current <= n) {
if (current % 3 != 0 && current % 5 != 0) {
printNumber.accept(current);
current += 1;
notifyAll();
} else {
wait();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ public class CombinationIterator {
private List<String> list;
private int index;
private int combinationLength;
private boolean[] visited;

public CombinationIterator(String characters, int combinationLength) {
this.index = 0;
this.list = new ArrayList<>();
this.combinationLength = combinationLength;
this.visited = new boolean[characters.length()];
boolean[] visited = new boolean[characters.length()];
buildAllCombinations(characters, 0, new StringBuilder(), visited);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public boolean isPossible(int[] target) {
|| target[maxIndex] % remainingSum == 0) {
return false;
}
target[maxIndex] %= remainingSum;
target[maxIndex] %= (int) remainingSum;
return isPossible(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class Solution {
public int[] countSubTrees(int n, int[][] edges, String labelsString) {
int[] labelsCount = new int[n];
if (n <= 0 || edges == null || labelsString == null) {
if (n == 0 || edges == null || labelsString == null) {
return labelsCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public int maxProfit(int[] inventory, int orders) {
long diff = i == 0 ? inventory[i] : inventory[i] - inventory[i - 1];
if (count * diff < orders) {
totalValue += (2L * inventory[i] - diff + 1) * diff * count / 2 % mod;
orders -= count * diff;
orders -= (int) (count * diff);
} else {
diff = orders / count;
long remainder = orders % count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public String getSmallestString(int n, int k) {
Arrays.fill(res, 'a');
k -= n;
while (k > 0) {
res[--n] += Math.min(25, k);
res[--n] += (char) Math.min(25, k);
k -= Math.min(25, k);
}
return String.valueOf(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private long getSum(int[] arr, int l, int h, long[] sum) {
int mid = l + (h - l) / 2;
int k = h - l + 1;
int radius = mid - l;
long res = sum[h + 1] - sum[mid + 1] - (sum[mid] - sum[l]) - (1 + radius) * radius;
long res = sum[h + 1] - sum[mid + 1] - (sum[mid] - sum[l]) - (long) (1 + radius) * radius;
if (k % 2 == 0) {
res = res - arr[mid] - (radius + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public boolean isCovered(int[][] ranges, int left, int right) {
int start = range[0];
int end = range[ranges[0].length - 1];
temp[start] += 1;
temp[end + 1] += -1;
temp[end + 1] -= 1;
}
for (int i = 1; i < temp.length; i++) {
temp[i] += temp[i - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private long[] dfs(int root) {
long com = 1;
for (long[] p : list) {
long choose = c(cnt, (int) (p[0]));
cnt -= p[0];
cnt -= (int) p[0];
com = com * choose;
com %= MOD;
com = com * p[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public int[] getAverages(int[] nums, int k) {
return res;
}
long sum = 0;
long range = 2 * k + 1L;
long range = 2L * k + 1L;
// take sum of all elements from 0 to k*2 index
for (int i = 0; i <= 2 * k; ++i) {
sum += nums[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public long subArrayRanges(int[] nums) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum += 1L * (cur - left) * (right - cur) * nums[cur];
sum += (long) (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
Expand All @@ -29,7 +29,7 @@ public long subArrayRanges(int[] nums) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum -= 1L * (cur - left) * (right - cur) * nums[cur];
sum -= (long) (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public int largestInteger(int num) {
str[i] = temp;
str[swapIndex] = tempStr;
}
return Integer.valueOf(new String(str));
return Integer.parseInt(new String(str));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int[] withdraw(int amount) {
return new int[] {-1};
}
for (int i = 0; i < 5; i++) {
counts[i] += -delivery[i];
counts[i] -= delivery[i];
}
return delivery;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public long appealSum(String s) {
long res = 0;
for (int i = 0; i < len; i++) {
int idx = s.charAt(i) - 'a';
res += (i - lastPos[idx]) * (len - i);
res += (long) (i - lastPos[idx]) * (len - i);
lastPos[idx] = i;
}
return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private String applyDiscount(String s, int discount) {
return s;
}
price *= 10;
price += (s.charAt(i) - '0') * (100 - discount);
price += (long) (s.charAt(i) - '0') * (100 - discount);
}
String stringPrice = String.valueOf(price);
if (price < 10) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public long minSumSquareDiff(int[] nums1, int[] nums2, int k1, int k2) {
// if current group has more differences than the totalK, we can only move k of them
// to the lower level.
if (diffs[i] >= kSum) {
diffs[i] -= kSum;
diffs[i - 1] += kSum;
diffs[i] -= (int) kSum;
diffs[i - 1] += (int) kSum;
kSum = 0;
} else {
// else, we can make this whole group one level lower.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public int maximumSum(int[] nums) {
for (int num : nums) {
int s = 0;
for (char digit : String.valueOf(num).toCharArray()) {
s += Integer.valueOf(digit - '0');
s += digit - '0';
}
if (!map.containsKey(s)) {
map.put(s, num);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public int countDaysTogether(
Integer endMonth = Integer.valueOf(ends[0]);
int res = 0;
if (startMonth.equals(endMonth)) {
res += (Integer.valueOf(ends[1]) - Integer.valueOf(starts[1]) + 1);
res += (Integer.parseInt(ends[1]) - Integer.parseInt(starts[1]) + 1);
return res;
}
for (int i = startMonth; i <= endMonth; i++) {
if (i == endMonth) {
res += Integer.valueOf(ends[1]);
res += Integer.parseInt(ends[1]);
} else if (i == startMonth) {
res += dates[i] - Integer.valueOf(starts[1]) + 1;
res += dates[i] - Integer.parseInt(starts[1]) + 1;
} else {
res += dates[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private boolean canIBeTheMinimum(long[] power, long minimum, int k, int r) {
if (req > k) {
return false;
}
k -= req;
k -= (int) req;
extraPower[i] += (req);
if (i + 2 * r + 1 < n) {
extraPower[i + 2 * r + 1] -= (req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private long pow(long x, long n, long mod) {
public int stringCount(int n) {
long mod = (int) 1e9 + 7L;
return (int)
(((+pow(26, n, mod)
(((pow(26, n, mod)
- (n + 75) * pow(25, n - 1L, mod)
+ (2 * n + 72) * pow(24, n - 1L, mod)
- (n + 23) * pow(23, n - 1L, mod))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private long smallPower(int[] power, int maxPower) {
dp[1] = counts[1];
dp[2] = Math.max(counts[2] * 2L, dp[1]);
for (int i = 3; i <= maxPower; i++) {
dp[i] = Math.max(counts[i] * i + dp[i - 3], Math.max(dp[i - 1], dp[i - 2]));
dp[i] = Math.max((long) counts[i] * i + dp[i - 3], Math.max(dp[i - 1], dp[i - 2]));
}
return dp[maxPower];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ void fizzBuzz() throws InterruptedException {
new Thread(
() -> {
try {
fizzBuzz.number(
value -> {
fizz[0]++;
});
fizzBuzz.number(value -> fizz[0]++);
} catch (InterruptedException e) {
}
})
Expand Down Expand Up @@ -82,10 +79,7 @@ void fizzBuzz2() throws InterruptedException {
new Thread(
() -> {
try {
fizzBuzz.number(
value -> {
fizz[0]++;
});
fizzBuzz.number(value -> fizz[0]++);
} catch (InterruptedException e) {
}
})
Expand Down

0 comments on commit 818ed59

Please sign in to comment.