-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90908ef
commit 9fcd785
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class Solution { | ||
public: | ||
int hammingWeight(uint32_t n) { | ||
int cnt = 0; | ||
while(n>0) | ||
{ | ||
if(n&1) cnt++; | ||
n >>= 1; | ||
} | ||
return cnt; | ||
} | ||
}; | ||
//-------------------- | ||
class Solution { | ||
public: | ||
int hammingWeight(uint32_t n) { | ||
int cnt = 0; | ||
while(n > 0) | ||
{ | ||
int bit = n % 10; | ||
if(bit == 1) cnt++; | ||
n/=10; | ||
//cout<<n<<" "; | ||
} | ||
return cnt; | ||
} | ||
}; | ||
|
||
// ---- Another approach | ||
|
||
class Solution { | ||
public: | ||
int hammingWeight(uint32_t n) { | ||
int cnt = 0 , t = 0, tt = 32; | ||
while(tt--) | ||
{ | ||
int x = 1<<t; | ||
if(x & n) cnt++; | ||
t++; | ||
} | ||
return cnt; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <bits/stdc++.h> | ||
typedef long long ll; | ||
using namespace std; | ||
static ll p = 1; | ||
class Solution { | ||
|
||
public: | ||
|
||
bool isPowerOfTwo(ll n) { | ||
if(n == p) | ||
{ | ||
p =1; | ||
return 1; | ||
} | ||
if(p > n) | ||
{ p =1; | ||
return 0; | ||
} | ||
p*=2; | ||
return isPowerOfTwo(n); | ||
} | ||
}; | ||
|
||
//--------------------------------- | ||
#include <bits/stdc++.h> | ||
typedef long long ll; | ||
using namespace std; | ||
class Solution { | ||
|
||
public: | ||
|
||
bool isPowerOfTwo(ll n) { | ||
ll cnt = 0; | ||
while(n>0) | ||
{ | ||
if(n&1) cnt++; | ||
n >>= 1; | ||
} | ||
if(cnt == 1) return 1; | ||
return 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
typedef long long ll; | ||
class Solution { | ||
public: | ||
double myPow(double x,ll n) { | ||
if(n < 0) x = 1/x , n *= -1; | ||
if(!n) return 1; | ||
if(n&1) return x * myPow(x*x,n>>1); | ||
return myPow(x*x,n>>1); | ||
} | ||
}; |