Skip to content

Commit

Permalink
add 338 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 12, 2024
1 parent a55ea2b commit b8b8ba1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
292| [Nim Game](https://leetcode.com/problems/nim-game/) |[Python](./leetcode_python/Math/nim-game.py) | _O(1)_ | _O(1)_ | Easy | LintCode | OK*
319 | [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | [Python](./leetcode_python/Math/bulb-switcher.py) | _O(1)_ | _O(1)_ | Medium || AGAIN
326 | [Power of Three](https://leetcode.com/problems/power-of-three/) | [Python](./leetcode_python/Math/power-of-three.py) | _O(1)_ | _O(1)_ | Easy || OK
338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [Python](./leetcode_python/Math/counting-bits.py) | _O(n)_ | _O(n)_ | Medium |Curated Top 75| OK*
338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [Python](./leetcode_python/Math/counting-bits.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java) | _O(n)_ | _O(n)_ | Medium |Curated Top 75| OK
343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [Python](./leetcode_python/Math/integer-break.py) | _O(logn)_ | _O(1)_ | Medium |`trick`, `DP` | AGAIN* (2)
365 | [Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/) | [Python](./leetcode_python/Math/water-and-jug-problem.py) | _O(logn)_ | _O(1)_ | Medium | `Bézout's identity` | AGAIN (not start)
372 | [Super Pow](https://leetcode.com/problems/super-pow/) | [Python](./leetcode_python/Math/super-pow.py) | _O(n)_ | _O(1)_ | Medium || AGAIN
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240313: 338
20240312: 297(again),300,322(again),323
20240311: 261(again!!!),268,269(again),271,295(again)
20240310: 213,217,226,230,235,238,242,252,253
Expand Down
16 changes: 9 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-05-07 -> ['338']
2024-05-06 -> ['297(again),300,322(again),323']
2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)']
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
Expand All @@ -11,6 +12,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-16 -> ['338']
2024-04-15 -> ['297(again),300,322(again),323']
2024-04-14 -> ['261(again!!!),268,269(again),271,295(again)']
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
Expand All @@ -21,27 +23,27 @@
2024-04-08 -> ['100,102,104,105(again)']
2024-04-07 -> ['73,76,79(again),91,25']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-03 -> ['338', '39,48(again),49,53,54']
2024-04-02 -> ['297(again),300,322(again),323', '20,21,23,33(again)']
2024-04-01 -> ['261(again!!!),268,269(again),271,295(again)', '1,3,5,4,19']
2024-03-31 -> ['213,217,226,230,235,238,242,252,253']
2024-03-30 -> ['207(again!!!),208,211(again),212']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-26 -> ['338', '100,102,104,105(again)']
2024-03-25 -> ['297(again),300,322(again),323', '73,76,79(again),91,25']
2024-03-24 -> ['261(again!!!),268,269(again),271,295(again)', '55(again),56,62,70']
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-21 -> ['338', '152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['297(again),300,322(again),323', '139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['261(again!!!),268,269(again),271,295(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-18 -> ['338', '213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-17 -> ['297(again),300,322(again),323', '207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-16 -> ['338', '261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['338', '297(again),300,322(again),323', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['338', '297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['297(again),300,322(again),323', '261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
Expand Down
60 changes: 60 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package LeetCodeJava.Math;

// https://leetcode.com/problems/counting-bits/

public class CountingBits {

// V0
// IDEA : MATH
public int[] countBits(int n) {
/** NOTE !!! we init ans as below */
int[] ans = new int[n+1];
for (int x = 0; x < n+1; x++){
// https://stackoverflow.com/questions/2406432/converting-an-int-to-a-binary-string-representation-in-java
String binary = Integer.toBinaryString(x);
ans[x] = countElement(binary, '1');
}

return ans;
}

// https://www.baeldung.com/java-count-chars
private int countElement(String s, char x){
int count = 0;
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == x){
count += 1;
}
}
return count;
}

// V1
// https://leetcode.com/problems/counting-bits/solutions/3987279/java-solution-using-right-shift-operator/
public int countOnes(int n) {
int count = 0;
while(n != 0) {
count += (n&1);
n = n>>1;
}
return count;
}
public int[] countBits_1(int n) {
int ans[] = new int[n+1];
ans[0] = 0;
for(int i=1; i<ans.length; i++) {
ans[i] = countOnes(i);
}
return ans;
}

// V2
// IDEA : BIT OP
// https://leetcode.com/problems/counting-bits/solutions/79539/three-line-java-solution/
public int[] countBits_2(int num) {
int[] f = new int[num + 1];
for (int i=1; i<=num; i++) f[i] = f[i >> 1] + (i & 1);
return f;
}

}

0 comments on commit b8b8ba1

Please sign in to comment.