https://leetcode.com/problems/number-of-even-and-odd-bits/description/
Number of Even and Odd Bits - LeetCode
Can you solve this real interview question? Number of Even and Odd Bits - You are given a positive integer n. Let even denote the number of even indices in the binary representation of n (0-indexed) with value 1. Let odd denote the number of odd indices in
leetcode.com
1
2
3
4
5
6
7
8
9
10
11
|
class Solution:
def evenOddBit(self, n: int) -> List[int]:
bits = (bin(n)[2:])[::-1]
even, odd = 0, 0
for i, b in enumerate(bits):
if i % 2 == 0 and b == '1':
even +=1
elif i % 2 == 1 and b == '1':
odd +=1
answer = [even, odd]
return answer
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2651. Calculate Delayed Arrival Time (0) | 2023.07.06 |
---|---|
leetcode 2544. Alternating Digit Sum (0) | 2023.07.03 |
leetcode 2716. Minimize String Length (0) | 2023.06.27 |
leetcode 2710. Remove Trailing Zeros From a String (0) | 2023.06.26 |
leetcode 2656. Maximum Sum With Exactly K Elements (0) | 2023.06.26 |
댓글