Skip to content

Commit

Permalink
adding more folders -- windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed-Arafat10 committed Sep 9, 2022
1 parent 90908ef commit 9fcd785
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Bit Manipulation/Easy/191. Number of 1 Bits.cpp
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;
}
};
42 changes: 42 additions & 0 deletions Bit Manipulation/Easy/231. Power of Two.cpp
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;
}
};
10 changes: 10 additions & 0 deletions Math/Medium/Leetcode 50. Pow(x, n).cpp
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);
}
};

0 comments on commit 9fcd785

Please sign in to comment.