mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Add solution to 'a lot' of problems
This commit is contained in:
17
Easy/reverse-bits/solution.cpp
Normal file
17
Easy/reverse-bits/solution.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
uint32_t reverseBits(uint32_t n) {
|
||||
uint32_t r = 0;
|
||||
for (int i = 31; i >= 0; --i) {
|
||||
if (n / (uint32_t)std::pow(2, i) >= 1) {
|
||||
n -= (uint32_t)std::pow(2, i);
|
||||
r += (uint32_t)std::pow(2, 31 - i);
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user