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.


Example 1:

Input: [0,1,1]
Output: [true,false,false]
Explanation:
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.

Example 2:

Input: [1,1,1]
Output: [false,false,false]

Example 3:

Input: [0,1,1,1,1,1]
Output: [true,false,false,false,true,false]

Example 4:

Input: [1,1,1,0,1]
Output: [false,false,false,false,false]

Note:

  1. 1 <= A.length <= 30000
  2. A[i] is 0 or 1

Idea 1. Modular arithmetic

(a*b + c)%d = (a%d) * (b%d) + c%d

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public List<Boolean> prefixesDivBy5(int[] A) {
int val = 0;
List<Boolean> divisible = new ArrayList<>(); for(int num: A) {
val = ((val * 2)%5 + num)%5;
divisible.add(val == 0? true : false);
} return divisible;
}
}

Idea 1.b left shift, bitwise operation

 class Solution {
public List<Boolean> prefixesDivBy5(int[] A) {
int val = 0;
List<Boolean> divisible = new ArrayList<>(); for(int num: A) {
val = ((val << 1) | num)%5;
divisible.add(val == 0);
} return divisible;
}
}

Binary Prefix Divisible By 5 LT1018的更多相关文章

  1. 【LeetCode】1018. Binary Prefix Divisible By 5 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [Swift]LeetCode1018. 可被 5 整除的二进制前缀 | Binary Prefix Divisible By 5

    Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a bi ...

  3. 【leetcode】1018. Binary Prefix Divisible By 5

    题目如下: Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted a ...

  4. 1018. Binary Prefix Divisible By 5可被 5 整除的二进制前缀

    网址:https://leetcode.com/problems/binary-prefix-divisible-by-5/ 一次for循环遍历数组,在上次计算的基础上得到本次的结果! class S ...

  5. Leetcode 1018. Binary Prefix Divisible By 5

    class Solution: def prefixesDivBy5(self, A: List[int]) -> List[bool]: ans,t = [],0 for a in A: t ...

  6. LeetCode.1018-可被5整除的二进制数(Binary Prefix Divisible By 5)

    这是小川的第379次更新,第407篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第241题(顺位题号是1018).给定0和1的数组A,考虑N_i:从A[0]到A[i]的第 ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. Weekly Contest 130

    1029. Binary Prefix Divisible By 5 Given an array A of 0s and 1s, consider N_i: the i-th subarray fr ...

  9. 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747

    真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...

随机推荐

  1. 第二篇:Jmeter功能概要

    一.jmeter工具组成部分: 1.资源生成器:用于生成测试过程中服务器,负载机的资源代码: 2.用户运行器:通常是一个脚本运行引擎,根据脚本的要求模拟指定用户行为,(lr中的controller) ...

  2. jms版本

    Java消息服务是一个在 Java标准化组织(JCP)内开发的标准(代号JSR 914). 2001年6月25日,Java消息服务发布JMS 1.0.2b,2002年3月18日Java消息服务发布 1 ...

  3. because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled

    spring security 配置问题,静态资源未被允许访问

  4. Python程序打包—pyinstaller

    简介:PyInstaller是一个十分有用的第三方库,通过对源文件打包,Python程序可以在没有安装 Python的环境中运行,也可以作为一个独立文件方便传递和管理. PyInstaller的官方网 ...

  5. PHP实现curl和snoopy类模拟登陆方法

    Snoopy.class.php下载 方法/步骤   第一种:使用snoopy类实现模拟登陆 1.在网上下载一个Snoopy.class.php的文件   2.代码实现: <?php set_t ...

  6. Android使用VideoView播放本地视频及网络视频Demo

    1.xm文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  7. Delphi: 获取控件文本宽度(像素)

    为适应多语言,需要对界面控件大小.位置多动态改变,因此需要根据其Caption计算实际像素大小. 找资料未有易用现成的,遂参数其它方法,写以函数处之,代码如下: uses TypInfo; funct ...

  8. sql语句Order by 报错列名不明确

    select top 10 column1,column2,column3 from table1 where table1.id not in(select top 0 table1.id from ...

  9. win8 IIS配置

    iis发展到win8已经到是iis8.0了.至于如何安装的,咱们接下来看.大体和iis6安装差别不大.iis8安装起来也是很方便的. 1.打开“控制面板”——“程序和功能”.如下图所示 2.点击“启用 ...

  10. day 09 函数的进阶

    01 动态参数 *args **kwargs 在函数的定义时,* ** 代表聚合. def func(**kwargs): print(kwargs) func(**{"name" ...