Skip to content

Commit

Permalink
update 338 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 18, 2024
1 parent dae0ea4 commit 2eae79a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 32 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240518: 213(todo),198(todo),212(todo),211
20240518: 213(todo),198(todo),212(todo),211,338
20240517: 347,253(todo),91(todo),217
20240516: 226,98,253(todo)
20240515: 104,230,102,100
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-07-12 -> ['213(todo),198(todo),212(todo),211']
2024-07-12 -> ['213(todo),198(todo),212(todo),211,338']
2024-07-11 -> ['347,253(todo),91(todo),217']
2024-07-10 -> ['226,98,253(todo)']
2024-07-09 -> ['104,230,102,100']
Expand All @@ -7,7 +7,7 @@
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-21 -> ['213(todo),198(todo),212(todo),211']
2024-06-21 -> ['213(todo),198(todo),212(todo),211,338']
2024-06-20 -> ['347,253(todo),91(todo),217']
2024-06-19 -> ['226,98,253(todo)']
2024-06-18 -> ['104,230,102,100']
Expand All @@ -16,27 +16,27 @@
2024-06-15 -> ['371']
2024-06-14 -> ['121,252']
2024-06-13 -> ['125']
2024-06-08 -> ['213(todo),198(todo),212(todo),211']
2024-06-08 -> ['213(todo),198(todo),212(todo),211,338']
2024-06-07 -> ['347,253(todo),91(todo),217']
2024-06-06 -> ['226,98,253(todo)']
2024-06-05 -> ['104,230,102,100']
2024-06-04 -> ['105,106']
2024-06-03 -> ['242,235']
2024-06-02 -> ['371']
2024-06-01 -> ['121,252']
2024-05-31 -> ['213(todo),198(todo),212(todo),211', '125']
2024-05-31 -> ['213(todo),198(todo),212(todo),211,338', '125']
2024-05-30 -> ['347,253(todo),91(todo),217']
2024-05-29 -> ['226,98,253(todo)']
2024-05-28 -> ['104,230,102,100']
2024-05-27 -> ['105,106']
2024-05-26 -> ['213(todo),198(todo),212(todo),211', '242,235']
2024-05-26 -> ['213(todo),198(todo),212(todo),211,338', '242,235']
2024-05-25 -> ['347,253(todo),91(todo),217', '371']
2024-05-24 -> ['226,98,253(todo)', '121,252']
2024-05-23 -> ['213(todo),198(todo),212(todo),211', '104,230,102,100', '125']
2024-05-23 -> ['213(todo),198(todo),212(todo),211,338', '104,230,102,100', '125']
2024-05-22 -> ['347,253(todo),91(todo),217', '105,106']
2024-05-21 -> ['213(todo),198(todo),212(todo),211', '226,98,253(todo)', '242,235']
2024-05-20 -> ['213(todo),198(todo),212(todo),211', '347,253(todo),91(todo),217', '104,230,102,100', '371']
2024-05-19 -> ['213(todo),198(todo),212(todo),211', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252']
2024-05-21 -> ['213(todo),198(todo),212(todo),211,338', '226,98,253(todo)', '242,235']
2024-05-20 -> ['213(todo),198(todo),212(todo),211,338', '347,253(todo),91(todo),217', '104,230,102,100', '371']
2024-05-19 -> ['213(todo),198(todo),212(todo),211,338', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252']
2024-05-18 -> ['347,253(todo),91(todo),217', '226,98,253(todo)', '104,230,102,100', '242,235', '125']
2024-05-17 -> ['226,98,253(todo)', '104,230,102,100', '105,106', '371']
2024-05-16 -> ['104,230,102,100', '105,106', '242,235', '121,252']
Expand Down
31 changes: 31 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Math/CountingBits.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,37 @@ private int countElement(String s, char x){
return count;
}

// V0
// IDEA : java default
public int[] countBits_0(int n) {

if (n == 0){
return new int[]{0};
}

int[] res = new int[n+1];

for (int i = 0; i < n+1; i++){
/**
* Integer.bitCount
*
* -> java default get number of "1" from binary representation of a 10 based integer
*
* -> e.g.
* Integer.bitCount(0) = 0
* Integer.bitCount(1) = 1
* Integer.bitCount(2) = 1
* Integer.bitCount(3) = 2
*
* Ref
* - https://blog.csdn.net/weixin_42092787/article/details/106607426
*/
int cnt = Integer.bitCount(i);
res[i] = cnt;
}
return res;
}

// V1
// https://leetcode.com/problems/counting-bits/solutions/3987279/java-solution-using-right-shift-operator/
public int countOnes(int n) {
Expand Down
84 changes: 62 additions & 22 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,44 @@ public static void main(String[] args) {



// Small PQ (default min-heap)
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();

// Big PQ (max-heap)
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());

// Add elements to PQs
smallPQ.add(5);
smallPQ.add(10);
smallPQ.add(1);

bigPQ.add(5);
bigPQ.add(10);
bigPQ.add(1);
// // Small PQ (default min-heap)
// PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
//
// // Big PQ (max-heap)
// PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
//
// // Add elements to PQs
// smallPQ.add(5);
// smallPQ.add(10);
// smallPQ.add(1);
//
// bigPQ.add(5);
// bigPQ.add(10);
// bigPQ.add(1);
//
// // Print elements from PQs
// System.out.println("Small PQ (min-heap):");
// while (!smallPQ.isEmpty()) {
// System.out.println(smallPQ.poll());
// }
//
// System.out.println("Big PQ (max-heap):");
// while (!bigPQ.isEmpty()) {
// System.out.println(bigPQ.poll());
// }

// Print elements from PQs
System.out.println("Small PQ (min-heap):");
while (!smallPQ.isEmpty()) {
System.out.println(smallPQ.poll());
}
for (int i = 0; i <= 3; i++){
int b_cnt = Integer.bitCount(i);

System.out.println("Big PQ (max-heap):");
while (!bigPQ.isEmpty()) {
System.out.println(bigPQ.poll());
/**
*
* i = 0 , b_cnt = 0
* i = 1 , b_cnt = 1
* i = 2 , b_cnt = 1
* i = 3 , b_cnt = 2
*
*/
System.out.println("i = " + i + " , b_cnt = " + b_cnt);
}

}
Expand Down Expand Up @@ -739,6 +753,32 @@ public boolean search_sub(String word, TrieNode trie) {

}

// LC 338
public int[] countBits(int n) {
if (n == 0){
return new int[]{0};
}

int[] res = new int[n+1];

for (int i = 0; i < n+1; i++){
/**
* Integer.bitCount
*
* -> java default get number of "1" from binary representation of a 10 based integer
*
* -> e.g.
* Integer.bitCount(0) = 0
* Integer.bitCount(1) = 1
* Integer.bitCount(2) = 1
* Integer.bitCount(3) = 2
*/
int cnt = Integer.bitCount(i);
res[i] = cnt;
}
return res;
}



}

0 comments on commit 2eae79a

Please sign in to comment.