mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 06:24:07 +00:00
day 10: 190-reverse-bits
so far was on easy, moving to medium difficulty
This commit is contained in:
parent
01f1ea79b0
commit
692280761f
10
190-reverse-bits/driver.cpp
Normal file
10
190-reverse-bits/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
uint32_t n = 43261596; // 00000010100101000001111010011100
|
||||
uint32_t answer = 964176192; // 00111001011110000010100101000000
|
||||
Solution soln;
|
||||
cout << "Reversed bits correctly? " << soln.test(n, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
18
190-reverse-bits/soln.cpp
Normal file
18
190-reverse-bits/soln.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
uint32_t Solution::reverseBits(uint32_t n)
|
||||
{
|
||||
uint32_t result = 0;
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
result = result << 1; // increase result size
|
||||
result += (n & 1); // append last bit
|
||||
n = n >> 1; // move to next bit
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Solution::test(uint32_t n, uint32_t answer)
|
||||
{
|
||||
return reverseBits(n) == answer;
|
||||
}
|
||||
10
190-reverse-bits/soln.hpp
Normal file
10
190-reverse-bits/soln.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
uint32_t reverseBits(uint32_t n);
|
||||
bool test(uint32_t n, uint32_t answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user