Skip to content

Commit b9c205f

Browse files
authored
Bit Manipulation C++ Solutions (#160)
* Add C++ Solutions for Bit Manipulation * Update README.md
1 parent ca13ec7 commit b9c205f

5 files changed

+97
-4
lines changed

Diff for: C++/Detect-Capital.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Problem URL: https://leetcode.com/problems/single-number-iii/
2+
/*
3+
* Runtime - 0ms
4+
* Memory - 5.9mb
5+
*/
6+
7+
class Solution {
8+
public:
9+
bool detectCapitalUse(string word) {
10+
bool all_caps = 1;
11+
bool all_small = 1;
12+
bool first_letter_caps = isupper(word[0]);
13+
14+
for(int i=1; i<word.length() && (all_caps | all_small); i++) {
15+
islower(word[i])? all_caps = 0: all_small = 0;
16+
}
17+
return (all_small | (first_letter_caps & all_caps));
18+
}
19+
};

Diff for: C++/Number-Complement.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Problem URL: https://leetcode.com/problems/single-number-iii/
2+
/*
3+
* Runtime - 0ms
4+
* Memory - 5.8mb
5+
*/
6+
7+
class Solution {
8+
public:
9+
int findComplement(int num) {
10+
int inv = 0, iter = 0;
11+
int reminder;
12+
while(num) {
13+
inv = inv | ((1 - (num % 2))<<iter);
14+
num /= 2;
15+
iter++;
16+
}
17+
return inv;
18+
}
19+
};
20+

Diff for: C++/Single-Number-II.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Leetcode URL: https://leetcode.com/problems/single-number-ii
2+
/*
3+
* Runtime - 4ms
4+
* Memory - 9.5
5+
*/
6+
7+
class Solution {
8+
public:
9+
int singleNumber(vector<int>& nums) {
10+
int a=0, b=0;
11+
for(int num: nums) {
12+
a = b & (a^num);
13+
b = a | (b^num);
14+
}
15+
return b;
16+
}
17+
};

Diff for: C++/Single-Number-III.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Problem URL: https://leetcode.com/problems/single-number-iii/
2+
/*
3+
* Runtime - 8ms
4+
* Memory - 10mb
5+
*/
6+
7+
class Solution {
8+
public:
9+
vector<int> singleNumber(vector<int>& nums) {
10+
int xor_all = 0;
11+
// Get the XOR value of all the numbers in the vector
12+
for(int num: nums) xor_all ^= num;
13+
// xor_all will contain a^b, where a and b are repeated only once.
14+
// if value of a bit in xor is 1, then it means either a or b has
15+
// 1 in that position, but not both. We can use this to find the answer.
16+
int setbit = 1;
17+
// Find the first position in xor_all where the value is 1
18+
while((setbit & xor_all) == 0)
19+
setbit <<= 1;
20+
21+
vector<int> result(2);
22+
23+
// We basically split the numbers into two sets.
24+
// All numbers in first set will have a bit in the setbit position.
25+
// Second set of numbers, will have 0 in the setbit position.
26+
for(int num: nums) {
27+
if(num & setbit) {
28+
result[0] ^= num;
29+
} else {
30+
result[1] ^= num;
31+
}
32+
}
33+
34+
return result;
35+
}
36+
};

Diff for: README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
8383
| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial |
8484
| ---- | ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | ---------------------------------------- |
8585
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) <br> [C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
86-
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium | | |
87-
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
88-
| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) |
89-
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
86+
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Python](./Python/single-number-ii.py)<br/> [C++](./C++/Single-Number-II.cpp) | _O(n)_ | _O(1)_ | Medium | | |
87+
| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) <br/> [C++](./C++/Single-Number-III.cpp) | _O(n)_ | _O(1)_ | Medium | | |
88+
| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) <br/> [C++](./C++/Number-Complement.cpp) | _O(1)_ | _O(1)_ | Easy | | [Tutorial](https://youtu.be/6bp5V-O3zts) |
89+
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) <br/> [C++](./C++/Detect-Capital.cpp) | _O(n)_ | _O(1)_ | Easy | | |
9090
| 1486 | [XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/) | [Java](./Java/xor-op-in-array.java) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
9191

9292
<br/>
@@ -484,6 +484,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
484484
| [JeongDaHyeon](https://github.com/JeongDaHyeon) <br> <img src="https://avatars0.githubusercontent.com/u/48541114?s=460&v=4" width="100" height="100"> | South Korea | Java | [GitHub](https://github.com/JeongDaHyeon) |
485485
[Aysia](https://www.linkedin.com/in/aysiaelise/) <br> <img src="https://avatars.githubusercontent.com/u/70167431?s=460&u=1637be8636b6db6e35343ed9c1318c23e909b463&v=4" width="100" height="100"> | USA | JavaScript | [GitHub](https://github.com/aysiae)
486486
| [Poorvi Garg](https://github.com/POORVI111) <br> <img src="https://avatars.githubusercontent.com/u/68559217?s=400&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/POORVI111)
487+
| [Lakshmanan Meiyappan](https://laxmena.com) <br> <img src="https://avatars.githubusercontent.com/u/12819059?s=400&v=4" width="100" height="100"> | India | C++ |[Website - Blog](https://laxmena.com)<br/> [GitHub](https://github.com/laxmena) <br/> [LinekdIn](https://www.linkedin.com/in/lakshmanan-meiyappan/)
487488

488489

489490

0 commit comments

Comments
 (0)