-
[leetcode 75-13] Counting BitsAlgorithm 2023. 1. 30. 22:42
https://leetcode.com/problems/counting-bits/
Counting Bits - LeetCode
Counting Bits - Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i. Example 1: Input: n = 2 Output: [0,1,1] Explanation: 0 --> 0 1 --> 1 2 --> 10 Exam
leetcode.com
class Solution(object): def countBits(self, n): s = [] for i in range(n+1): s.append(bin(i).count("1")) return s
'Algorithm' 카테고리의 다른 글
[leetcode 75-15] Reverse Bits (0) 2023.01.30 [leetcode 75-14] Missing Number (0) 2023.01.30 [leetcode 75-12] Number of 1 Bits (0) 2023.01.30 [leetcode 75-11] Sum of Two Integers (0) 2023.01.30 [leetcode 75-8] Search in Rotated Sorted Array (1) 2023.01.30