-
Notifications
You must be signed in to change notification settings - Fork 2
/
1085. Sum of Digits in the Minimum Number.py
46 lines (40 loc) · 1.31 KB
/
1085. Sum of Digits in the Minimum Number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Given an array A of positive integers, let S be the sum of the digits of the minimal element of A.
# Return 0 if S is odd, otherwise return 1.
# Example 1:
# Input: [34,23,1,24,75,33,54,8]
# Output: 0
# Explanation:
# The minimal element is 1, and the sum of those digits is S = 1 which is odd, so the answer is 0.
# Example 2:
# Input: [99,77,33,66,55]
# Output: 1
# Explanation:
# The minimal element is 33, and the sum of those digits is S = 3 + 3 = 6 which is even, so the answer is 1.
# Note:
# 1 <= A.length <= 100
# 1 <= A[i].length <= 100
# Hints:
# How to find the minimum number in an array?
# Loop over the array and compare each one of the numbers.
# How to find the sum of digits?
# Divide the number consecutively and get their remainder modulus 10.
# Sum those remainders and return the answer as the problem asks.
class Solution(object):
def sumOfDigits(self, A):
"""
:type A: List[int]
:rtype: int
"""
# 找最小,求数位和,判断结果
mini = float('inf')
for a in A:
if a < mini:
mini = a
res = 0
while mini:
digit = mini % 10
mini /= 10
res += digit
return 0 if res%2 else 1
# 调库
# return 0 if sum(map(int,list(str(min(A))))) % 2 else 1