zoukankan      html  css  js  c++  java
  • leetcode 1018 可被5整除的二进制前缀

    package com.example.lettcode.dailyexercises;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Class PrefixesDivBy5
     * @Description 1018 可被5整除的二进制前缀
     * 给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个
     * 二进制数(从最高有效位到最低有效位)。
     * 返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false。
     * <p>
     * 示例 1:
     * 输入:[0,1,1]
     * 输出:[true,false,false]
     * 解释:
     * 输入数字为 0, 01, 011;也就是十进制中的 0, 1, 3 。只有第一个数可以被 5 整除,因此 answer[0] 为真。
     * <p>
     * 示例 2:
     * 输入:[1,1,1]
     * 输出:[false,false,false]
     * <p>
     * 示例 3:
     * 输入:[0,1,1,1,1,1]
     * 输出:[true,false,false,false,true,false]
     * <p>
     * 示例 4:
     * 输入:[1,1,1,0,1]
     * 输出:[false,false,false,false,false]
     * <p>
     * 提示:
     * 1 <= A.length <= 30000
     * A[i] 为 0 或 1
     * @Author
     * @Date 2021/1/14
     **/
    public class PrefixesDivBy5 {
        public static List<Boolean> prefixesDivBy5(int[] A) {
            if (A == null || A.length == 0) return new ArrayList<>();
    
            List<Boolean> booleanList = new ArrayList<>();
            int count = A[0];
            for (int i = 1; i < A.length; i++) {
                if (count % 5 == 0) booleanList.add(true);
                else booleanList.add(false);
                count = (count * 2 + A[i]) % 10;
            }
            if (count % 5 == 0) booleanList.add(true);
            else booleanList.add(false);
            return booleanList;
        }
    }
    
    // 测试用例
    public static void main(String[] args) {
    	int[] A = new int[]{0, 1, 1};
    	List<Boolean> booleanList = prefixesDivBy5(A);
    	System.out.print("PrefixesDivBy5 demo01 result:[");
    	for (Boolean ble : booleanList) {
    		System.out.print(" " + ble);
    	}
    	System.out.println("]");
    
    	A = new int[]{1, 1, 1};
    	booleanList = prefixesDivBy5(A);
    	System.out.print("PrefixesDivBy5 demo02 result:[");
    	for (Boolean ble : booleanList) {
    		System.out.print(" " + ble);
    	}
    	System.out.println("]");
    
    	A = new int[]{0, 1, 1, 1, 1, 1};
    	booleanList = prefixesDivBy5(A);
    	System.out.print("PrefixesDivBy5 demo03 result:[");
    	for (Boolean ble : booleanList) {
    		System.out.print(" " + ble);
    	}
    	System.out.println("]");
    
    	A = new int[]{1, 1, 1, 0, 1};
    	booleanList = prefixesDivBy5(A);
    	System.out.print("PrefixesDivBy5 demo04 result:[");
    	for (Boolean ble : booleanList) {
    		System.out.print(" " + ble);
    	}
    	System.out.println("]");
    }
    
  • 相关阅读:
    Recommended Books for Algo Trading in 2020
    Market Making is simpler than you think!
    Top Crypto Market Makers of 2020
    Top Crypto Market Makers, Rated and Reviewed
    爬取伯乐在线文章(五)itemloader
    爬取伯乐在线文章(四)将爬取结果保存到MySQL
    爬取伯乐在线文章(三)爬取所有页面的文章
    爬取伯乐在线文章(二)通过xpath提取源文件中需要的内容
    爬取伯乐在线文章(一)
    爬虫去重策略
  • 原文地址:https://www.cnblogs.com/fyusac/p/14275966.html
Copyright © 2011-2022 走看看