LeetCode solutions

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

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 7.8MB, accepted 2025-12-27.

cpp
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;
    }
};

Source