1054. Complement of Base 10 Integer
My accepted C++ solution to LeetCode problem 1054, Complement of Base 10 Integer, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 7.8MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 7.8MB, accepted 2025-12-27.
class Solution {
public:
int bitwiseComplement(int n) {
if (n == 0) return 1;
int mask = 1;
while (mask < n) mask = (mask << 1) | 1;
return n ^ mask;
}
};