Problems
Given an array nums of integers, return how many of them contain an even number of digits.
Example 1:
Input : nums = [12,345,2,6,7896]
Output : 2
Explanation
- 12 contains 2 digits (even number of digits).
- 345 contains 3 digits (odd number of digits).
- 2 contains 1 digit (odd number of digits).
- 6 contains 1 digit (odd number of digits).
- 7896 contains 4 digits (even number of digits).
- Therefore only 12 and 7896 contain an even number of digits.
Constraints:
- 1 <= nums.length <= 500
- 1 <= nums[i] <= 105
solution
class Solution:
def findNumbers(self, nums: List[int]) -> int:
cnt = 0
for i in nums:
if len(str(i)) % 2 == 0:
cnt += 1
return cnt
'알고리즘 > 기타 알고리즘' 카테고리의 다른 글
[LeetCode] Duplicate Zeros (Python) (0) | 2022.05.16 |
---|---|
[LeetCode] Squares of a Sorted Array (Python) (0) | 2022.05.16 |
[LeetCode] Max Consecutive Ones (Python) (0) | 2022.05.16 |
[이것이 코딩 테스트다 with Python] 고정점 찾기 : 이진탐색 (0) | 2022.01.24 |
[완전 탐색, 시뮬레이션] 시각 (0) | 2022.01.24 |