1018. Binary Prefix Divisible By 5

Given an array A of 0s and 1s, 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

 

posted @ 2020-07-13 21:14  whatyouthink  阅读(108)  评论(0编辑  收藏  举报