Skip to content

Commit 885b9f0

Browse files
committed
09/13
1 parent 47ecb0d commit 885b9f0

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Feel free to add issues, comment and pull request.
2525
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
2626
| Careercup | [Permutation Casewise](https://www.careercup.com/question?id=6255535581036544) | [Java](./java/PermuteCasewise.java) \| [Python](./Python/) | _O(2^n)_ | _O(1)_ | Medium | |
2727
| Leetcode | [461. Hamming Distance](https://leetcode.com/problems/hamming-distance/description/) | [Java](./java/hammingDistance.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
28+
| Leetcode | [338. Counting Bits](https://leetcode.com/problems/counting-bits/description/) | [Java](./java/countBits.java) \| [Python](./Python/) | _O(nk)_ | _O(n)_ | Medium | |
2829

2930

3031
## Arrays
@@ -86,6 +87,7 @@ Feel free to add issues, comment and pull request.
8687
| Website | Title | Solution | Time | Space | Difficulty | Note|
8788
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
8889
| Leetcode | [654. Maximum Binary Tree](https://leetcode.com/contest/leetcode-weekly-contest-44/problems/maximum-binary-tree/) | [Java](./java/ConstructMaximumBinaryTree.java) \| [Python](./Python/) | _O(n log n)_ | _O(n)_ | Easy | |
90+
| Leetcode | [50. Pow(x, n)](https://leetcode.com/problems/powx-n/description/) | [Java](./java/myPow.java) \| [Python](./Python/) | _O()_ | _O(n)_ | Medium | |
8991

9092

9193
## Recursion

java/countBits.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
The least significant bit which las 1 corresponds to 0 bit in n - 1.
3+
4+
AND N and N-1 and keep doing till you hit 0. Count the number of steps and store it in array
5+
6+
Time: O(nk) k - number of bits
7+
Space O(1)
8+
9+
*/
10+
11+
class Solution {
12+
public int[] countBits(int num) {
13+
14+
int [] result = new int[num+1];
15+
16+
for(int i=0; i<=num; i++)
17+
result[i]=hammingBits(i);
18+
return result;
19+
20+
}
21+
22+
public int hammingBits(int n)
23+
{
24+
int sum = 0;
25+
while(n !=0)
26+
{
27+
sum ++;
28+
n = n&n-1;
29+
}
30+
return sum;
31+
}
32+
}

java/myPow.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public double myPow(double x, int n) {
3+
4+
if(n==0) return 1;
5+
6+
if(n<0)
7+
{
8+
n = -n;
9+
x = 1/x;
10+
}
11+
12+
return n%2 == 0? myPow(x*x, n/2): x*myPow(x*x, n/2);
13+
14+
}
15+
}

0 commit comments

Comments
 (0)