【leetcode】1018. Binary Prefix Divisible By 5

news/2024/7/1 2:40:48

题目如下:

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

解题思路:本题很简单,往左移位即可。每移动一位,如果当前位置的值是1,值需要加上1。

代码如下:

class Solution(object):def prefixesDivBy5(self, A):""":type A: List[int]:rtype: List[bool]"""res = []val = 0for i in A:val = val << 1if i == 1:val += 1res.append(val % 5 == 0)return res

 

转载于:https://www.cnblogs.com/seyjs/p/10634961.html


http://lihuaxi.xjx100.cn/news/235895.html

相关文章

puppeteer爬虫的奇妙之旅

(爬虫)[puppeteer|] 爬虫又称网络机器人。每天或许你都会使用搜索引擎&#xff0c;爬虫便是搜索引擎重要的组成部分&#xff0c;爬取内容做索引。现如今大数据&#xff0c;数据分析很火&#xff0c;那数据哪里来呢&#xff0c;可以通过网络爬虫爬取啊。那我萌就来探讨一下网络爬…

linux常用运维工具uptime、iostat、vmstat、sar

目录 一、uptime 二、iostat 三、vmstat 四、sar 一、uptime uptime可以告诉你系统已经运行了多久。uptime命令回显一行信息&#xff0c;包括&#xff1a;系统运行了多久&#xff0c;目前有多少用户在登录&#xff0c;过去1、5、15分钟系统平均负载。这些内容和命令w回显的…

手持终端以物联网的模式

近年来&#xff0c;物联宇手持终端以物联网的模式&#xff0c;开启了信息化的管理模式&#xff0c;迸发了新的自我提升和业务新商机。手持终端是一款智能的电子设备&#xff0c;它的核心功能为用户速带来业务效率的提升&#xff0c;如快递行业&#xff0c;每天的工作量需求大&a…

Linux基础教程之linux文件权限深度解读

基本命令——来源于马哥教育官网1.cut: cat /etc/passwd | cut -d’:’ -f7| uniq -c| sort -nr 2.authconfig 修改加密方式–passalgosha256 — update3.scp 上传文件-r dir ip:path 传目录file ip:path传文件-P port 指定端口4.rsync 同步文件-avz 源文件 ip:pathscp和rsync都…

中国互联网公司开源项目调研报告

近年来&#xff0c;开源技术得到越来越多的重视&#xff0c;微软收购GitHub、IBM收购红帽&#xff0c;都表明了开源的价值。国内公司也越来越多的参与开源&#xff0c;加入开源基金会/贡献开源项目。但是&#xff0c;它们到底做得如何呢&#xff1f;为此InfoQ统计了国内在GitHu…

centos8编译openssl-1.0.2u、openssl-1.1.1k

目录 一、给openssl-1.0.2u打包rpm 二、编译安装openssl-1.1.1k 三、给openssl-1.1.1k打包rpm&#xff08;不推荐&#xff01;&#xff09; 近日openssl爆出拒绝服务、证书绕过漏洞&#xff0c;CVE编号CVE-2021-3449、CVE-2021-3450。 解决方法&#xff1a; CentOS7默认ope…

rabbitmq可靠发送的自动重试机制 --转

原贴地址 https://www.jianshu.com/p/6579e48d18ae https://www.jianshu.com/p/4112d78a8753 git项目代码地址 https://github.com/littlersmall/rabbitmq-access 转载于:https://www.cnblogs.com/hmpcly/p/10641688.html

【救援过程】升级openssl导致libcrypto.so.1.1动态库不可用

目录 一、故障重现 二、救援过程 一、故障重现 近日为了解决CVE-2021-3449: 拒绝服务漏洞、CVE-2021-3450: 证书校验漏洞&#xff0c;自己编译了openssl-1.1.1k。 亲测发现&#xff1a;只升级openssl的版本&#xff0c;动态库版本没有升级&#xff0c;系统可用。 升级openss…