https://leetcode.com/problems/complement-of-base-10-integer/description/
Complement of Base 10 Integer - LeetCode
Can you solve this real interview question? Complement of Base 10 Integer - The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation. * For example, The integer 5 is "101" in b
leetcode.com
1
2
3
4
5
6
7
8
9
10
11
|
class Solution:
def bitwiseComplement(self, n: int) -> int:
s = bin(n)[2:]
answer = ''
for i in s:
if i == '1':
answer += '0'
else:
answer += '1'
answer = int(answer, 2)
return answer
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
백준 27889 특별한 학교 이름 (0) | 2023.04.04 |
---|---|
leetcode 2348. Number of Zero-Filled Subarrays (0) | 2023.03.21 |
leetcode 2553. Separate the Digits in an Array (0) | 2023.03.14 |
leetcode 1122. Relative Sort Array (0) | 2023.03.13 |
leetcode 2395. Find Subarrays With Equal Sum (0) | 2023.03.13 |
댓글