Binary Prefix Divisible By 5 LT1018

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)

 1 class Solution {
 2     public List<Boolean> prefixesDivBy5(int[] A) {
 3         int val = 0;
 4         List<Boolean> divisible = new ArrayList<>();
 5
 6         for(int num: A) {
 7             val = ((val * 2)%5 + num)%5;
 8             divisible.add(val == 0? true : false);
 9         }
10
11         return divisible;
12     }
13 }

Idea 1.b left shift, bitwise operation

 1 class Solution {
 2     public List<Boolean> prefixesDivBy5(int[] A) {
 3         int val = 0;
 4         List<Boolean> divisible = new ArrayList<>();
 5
 6         for(int num: A) {
 7             val = ((val << 1) | num)%5;
 8             divisible.add(val == 0);
 9         }
10
11         return divisible;
12     }
13 }

原文地址:https://www.cnblogs.com/taste-it-own-it-love-it/p/10652723.html

时间: 2024-10-16 13:00:30

Binary Prefix Divisible By 5 LT1018的相关文章

Leetcode-1029 Binary Prefix Divisible By 5(可被 5 整除的二进制前缀)

1 class Solution 2 { 3 public: 4 vector<bool> prefixesDivBy5(vector<int>& A) 5 { 6 vector<bool> v(A.size()); 7 int rnt = 0; 8 for(int i = 0; i< A.size();i ++) 9 { 10 if(A[i]==1) 11 { 12 if(rnt==0) 13 {rnt = 1;v[i] = false;} 14 els

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 = (t * 2 + a)%5 ans.append(False if t else True) return ans 原文地址:https://www.cnblogs.com/zywscq/p/10739388.html

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

这是小川的第379次更新,第407篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第241题(顺位题号是1018).给定0和1的数组A,考虑N_i:从A[0]到A[i]的第i个子数组被解释为二进制数(从最高有效位到最低有效位). 返回布尔值answer列表,当且仅当N_i可被5整除时,answer[i]为true. 例如: 输入:[0,1,1] 输出:[true,false,false] 说明:二进制输入数字为0,01,011,转为十进制数,分别为0,1和3.只有第一

Weekly Contest 130

1029. 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 binary number (from most-significant-bit to least-significant-bit.) Return a list of booleans answer, where answer[i]

Flink - Juggling with Bits and Bytes

http://www.36dsj.com/archives/33650 http://flink.apache.org/news/2015/05/11/Juggling-with-Bits-and-Bytes.html http://www.bigsynapse.com/addressing-big-data-performance ,addressing-big-data-performance   第一篇描述,当前JVM存在的问题, 1. Java对象开销 Java对象的存储密度相对偏低,对

玩转Bits和Bytes(一)

How Apache Flink operates on binary data Nowadays, a lot of open-source systems for analyzing large data sets are implemented in Java or other JVM-based programming languages. The most well-known example is Apache Hadoop, but also newer frameworks su

UVA 11488 Hyper Prefix Sets (Trie)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483 Hyper Prefix Sets Prefix goodness of a set string islength of longest common prefix*number of strings in the set.For example the prefix goodnes

Swift在for循环中报错&#39;++&#39; is not a binary operator

最近刚开始学swift,遇到了一个非常诡异的问题.是在写for循环的时候出现语法错误.代码如下: for var i = 0; i < 10; i++{ println("hello world") } 按理说这是Swift里最简单的for循环的使用了.但是编译器还是报了两个错: '++ is not a binary operator' Operator is not a known binary operator 虽然苹果在官方文档里面说,建议通过++i这种方式使用自增运算符

HDU 11488 Hyper Prefix Sets (字符串-Trie树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes