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("]");
    }
    
  • 相关阅读:
    Ubuntu 下安装 PHP Solr 扩展的安装与使用
    转载:Ubuntu14-04安装redis和php5-redis扩展
    Datagridview全选,更新数据源代码
    sftp不识别的问题ssh命令找不到
    linux:如何修改用户的密码
    win7.wifi热点
    Rico Board.1.环境配置
    linux学习记录.6.vscode调试c makefile
    linux学习记录.5.git & github
    linux学习记录.3.virtualbox 共享文件夹
  • 原文地址:https://www.cnblogs.com/fyusac/p/14275966.html
Copyright © 2011-2022 走看看