Skip to content

Commit

Permalink
Reverse bits (#211)
Browse files Browse the repository at this point in the history
* add reverse bits

* impl reverse bits
  • Loading branch information
SKTT1Ryze authored Mar 18, 2024
1 parent f69e5d6 commit 593cbdc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
40 changes: 40 additions & 0 deletions leetcode-cc/ReverseBits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(PReverseBits, 190, DIFFI_EASY, TOPIC_ALGORITHMS,
"Reverse Bits",
"Reverse bits of a given 32 bits unsigned integer.",
{"Bit"});

class SReverseBits : public ISolution {
public:
size_t problemId() const override { return 190; }
string name() const override {
return ("Solution for " + string("Reverse Bits"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<uint32_t, uint32_t>(
{0b00000010100101000001111010011100,
0b11111111111111111111111111111101},
{0b00111001011110000010100101000000,
0b10111111111111111111111111111111},
[this](auto input) { return this->reverseBits(input); });
};
int benchmark() const override { return 0; }

private:
uint32_t reverseBits(uint32_t n) const {
const int N = 32;
uint32_t res = 0;

for (int i = 0; i < N; i++) {
res = (res << 1) + ((n >> i) & 0x1);
}

return res;
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#include "../leetcode-cc/ReorderList.hpp"
#include "../leetcode-cc/RepeatedDNASeq.hpp"
#include "../leetcode-cc/RestoreIpAddresses.hpp"
#include "../leetcode-cc/ReverseBits.hpp"
#include "../leetcode-cc/ReverseLinkedListII.hpp"
#include "../leetcode-cc/ReverseNodesinKGroup.hpp"
#include "../leetcode-cc/ReverseWords.hpp"
Expand Down Expand Up @@ -795,5 +796,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SRotateArray>(); });

handle->registerProblem(
[]() -> ArcProblem { return std::make_shared<PReverseBits>(); });
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SReverseBits>(); });

return 0;
}

0 comments on commit 593cbdc

Please sign in to comment.