Created
March 17, 2019 10:03
-
-
Save fpdjsns/a2f5812d1218065e20539f6cab6843ee to your computer and use it in GitHub Desktop.
[leetcode] 1012. Complement of Base 10 Integer : https://leetcode.com/problems/complement-of-base-10-integer/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int bitwiseComplement(int N) { | |
if(N == 0) return 1; | |
int tmp = 1; | |
int answer = 0; | |
while(0 < N){ | |
answer += ((N + 1) % 2) * tmp; | |
N /= 2; | |
tmp *= 2; | |
} | |
return answer; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment