LeetCode Solutions

476. Number Complement

Time: $O(32) = O(1)$

Space: $O(1)$

			

class Solution {
 public:
  int findComplement(long num) {
    for (long i = 1; i <= num; i <<= 1)
      num ^= i;
    return num;
  }
};
			

class Solution {
  public int findComplement(int num) {
    for (long i = 1; i <= num; i <<= 1)
      num ^= i;
    return num;
  }
}