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一次简单的接口测试(转载)

    转载自 http://www.cnblogs.com/yangxia-test 本次接口测试:根据ws查询所有商品的具体的信息.检查商品是否返回成功. 1.  准备测试数据 查询数据库中产品表已上架商 ...

  2. JMeter学习(三)元件的作用域与执行顺序(转载)

    转载自 http://www.cnblogs.com/yangxia-test 1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它 ...

  3. day17 正则表达式 re模块和hashlib模块

    今日内容 1. re&正则表达式(*****) 注:不要将自定义文件命名为re import re re.findall(正则表达式,被匹配的字符串) 拿着正则表达式去字符串中找,返回一个列表 ...

  4. Python图片识别找坐标(appium通过识别图片点击坐标)

    ***如果只想了解图片相似度识别,直接看第一步即可 ***如果想了解appium根据图片识别点击坐标,需要看第一.二.三步   背景|在做UI测试时,发现iOS自定义的UI控件,appium识别不到. ...

  5. 将秒数转为HH:MM:SS格式的时间

    /**  * 将秒数转为HH:MM:SS格式的时间  * @param $seconds  * @return string  */ public static function GetHHMMSSB ...

  6. ES6之扩展运算符

    一 将数组转换为用逗号分隔的参数序列 let turtles = ['Leo','Raph','Mikey','Don']; console.log(...turtles); 二 在解构赋值时使用 l ...

  7. TOJ 2778 数据结构练习题――分油问题(广搜和哈希)

    描述 设有大小不等的三个无刻度的油桶,分别能盛满x,y,z公升油.初始时,第一个油桶盛满油,第二.三个油桶为空,在某一个油桶上分出targ公升油. 输入 输入包含多组测试数据.每组数据包含一行.分别x ...

  8. 宋体freetype16和12号字无法正常显示

    在使用freetype过程中发现,从window下拷贝来的simsun.ttc, simkai.ttf两个字体, 在调用 FT_Set_Pixel_Sizes(face, 12, 0): 将字体大小设 ...

  9. day 20 类与类之间的关系,继承2

    类与类之间的关系: 依赖关系 关联关系 聚合关系 组合关系 依赖关系: 大象与冰箱的例子 # 依赖关系,冰箱依赖大象打开 class Elephant: def __init__(self,name) ...

  10. utils.js和vue-loader.conf.js

    var utils = require('./utils')var config = require('../config')var isProduction = process.env.NODE_E ...