2021-01-14 1018. 可被 5 整除的二进制前缀

1018. 可被 5 整除的二进制前缀

思路一:逐位模拟

因为每次左移=*2,所以我们下一次更新为tmp = (tmp%5)*2+A[i]

class Solution {
    
    
    public List<Boolean> prefixesDivBy5(int[] A) {
    
    
        List<Boolean> res = new ArrayList<Boolean>();
        int tmp=0; //用来存储当前数字除以5的余数
        for(int i=0;i<A.length;i++){
    
    
            tmp = (tmp%5)*2+A[i];
            res.add((tmp%5)==0);
        }
            
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44495738/article/details/112598569