-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add bitwise and of numbers range * impl bitwise and of numbers range
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
PBitWiseANDOfNumRange, 201, DIFFI_MEDIUM, TOPIC_ALGORITHMS, | ||
"Bit Wise AND Of Numbers Range", | ||
"Given two integers left and right that represent the range [left, right], " | ||
"return the bitwise AND of all numbers in this range, inclusive.", | ||
{"Bit"}); | ||
|
||
class SBitWiseANDOfNumRange : public ISolution { | ||
public: | ||
size_t problemId() const override { return 201; } | ||
string name() const override { | ||
return ("Solution for " + string("Bit Wise AND Of Numbers Range")); | ||
} | ||
string location() const override { return __FILE_NAME__; } | ||
int test() const override { | ||
return testHelper<pair<int, int>, int>( | ||
{{5, 7}, {0, 0}, {1, 2147483647}}, {4, 0, 0}, [this](auto input) { | ||
return this->rangeBitwiseAnd(input.first, input.second); | ||
}); | ||
}; | ||
int benchmark() const override { return 0; } | ||
|
||
private: | ||
int rangeBitwiseAnd(int left, int right) const { | ||
int shift = 0; | ||
while (left < right) { | ||
left >>= 1; | ||
right >>= 1; | ||
shift++; | ||
} | ||
|
||
return left << shift; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters