generated from zeikar/issueage
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
easyEasyEasy
Description
Problem link
https://leetcode.com/problems/number-complement/
Problem Summary
수가 주어질 때 complement를 구하는 문제.
Solution
1이 0이 되고 0이 1이 되려면?? XOR를 쓰면 된다.
자릿수를 올라가면서 1과 XOR를 하면 1은 0이 되고 0은 1이 된다.
Source Code
class Solution:
def findComplement(self, num: int) -> int:
b = 1
while num >= b:
num ^= b
b <<= 1
return num