美文网首页Leetcode
Leetcode 1018. Binary Prefix Div

Leetcode 1018. Binary Prefix Div

作者: SnailTyan | 来源:发表于2021-08-30 13:40 被阅读0次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Binary Prefix Divisible By 5

    2. Solution

    解析:Version 1,每次数组索引向右移动,相当于当前数字左移一位,因此当前数值应在上一数值的基础上乘以2,加上当前位。

    • Version 1
    class Solution:
        def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
            n = len(nums)
            pre = 0
            result = [0] * n
            for i in range(n):
                current = pre * 2 + nums[i]
                #current = (pre << 1) + nums[i]
                result.append(current % 5 == 0)
                pre = current
            return result
    

    Reference

    1. https://leetcode.com/problems/binary-prefix-divisible-by-5/

    相关文章

      网友评论

        本文标题:Leetcode 1018. Binary Prefix Div

        本文链接:https://www.haomeiwen.com/subject/ipamvltx.html