Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse bits #211

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading