首页 > 其他 > 详细

1018. Binary Prefix Divisible By 5

时间:2020-07-13 21:58:09      阅读:80      评论:0      收藏:0      [点我收藏+]

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

 

1018. Binary Prefix Divisible By 5

原文:https://www.cnblogs.com/whatyouthink/p/13295789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!