Skip to content

476. Number Complement 👍

  • Time: $O(32) = O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
class Solution {
 public:
  int findComplement(long num) {
    for (long i = 1; i <= num; i <<= 1)
      num ^= i;
    return num;
  }
};
1
2
3
4
5
6
7
class Solution {
  public int findComplement(int num) {
    for (long i = 1; i <= num; i <<= 1)
      num ^= i;
    return num;
  }
}