본문 바로가기
python-algorithm

leetcode 2595. Number of Even and Odd Bits

by 무적김두칠 2023. 6. 27.

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 = 00
        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
반응형

댓글