1018. Binary Prefix Divisible By 5
Given an array A
of 0
s and 1
s, consider N_i
: the i-th subarray from A[0]
to A[i]
interpreted as a binary number (from most-significant-bit to least-significant-bit.)
Return a list of booleans answer
, where answer[i]
is true
if and only if N_i
is divisible by 5.
一个二进制数组,问A[0]-A[i]组成的子数组,是否能被5整除。
能被5整除的数只需要看最后一位是不是5或者0就行。
class Solution(object): def prefixesDivBy5(self, A): """ :type A: List[int] :rtype: List[bool] """ ans = [] num = 0 for value in A: num <<= 1 num += value num %= 10 ans.append(num % 5 == 0) return ans