【leetcode】1018. Binary Prefix Divisible By 5
2024-10-07 14:09:25
题目如下:
Given an array
A
of0
s and1
s, considerN_i
: the i-th subarray fromA[0]
toA[i]
interpreted as a binary number (from most-significant-bit to least-significant-bit.)Return a list of booleans
answer
, whereanswer[i]
istrue
if and only ifN_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 <= A.length <= 30000
A[i]
is0
or1
解题思路:本题很简单,往左移位即可。每移动一位,如果当前位置的值是1,值需要加上1。
代码如下:
class Solution(object):
def prefixesDivBy5(self, A):
"""
:type A: List[int]
:rtype: List[bool]
"""
res = []
val = 0
for i in A:
val = val << 1
if i == 1:
val += 1
res.append(val % 5 == 0)
return res
最新文章
- 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(0)
- 剑指架构师系列-Hibernate需要掌握的Annotation
- flask中的session对象方法
- IIS启用.net2.0
- ubuntu 12.04 安装 codeblock 12.11
- STC-ISP下载过程
- (转)关于c#中的事件
- java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception
- IOC框架之一Autofac
- BST(Binary Search Tree)
- 【CSS】思考和再学习——关于CSS中浮动和定位对元素宽度/外边距/其他元素所占空间的影响
- Docker 初步认识
- JQ在线引用地址
- canvas图表详解系列(4):动态散点图
- ES6标准入门 第一章:简介
- org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found :
- GDAL指定自定义的金字塔目录
- 【Spring学习笔记-MVC】Spring MVC之多文件上传 (zhan)
- POJ 1308 Is It A Tree?和HDU 1272 	小希的迷宫
- jquery方法.serializeArray()获取name和value并转为json数组
热门文章
- Spring Security 3.1 中功能强大的加密工具 PasswordEncoder
- POI 单元格类型CellType
- Jmeter从数据库中读取数据
- Run Your Tensorflow Deep Learning Models on Google AI
- JS获取当前时间并格式化
- OSPF协议——原理及实验
- spring事务——try{...}catch{...}中事务不回滚的几种处理方式(转载)
- Mysql 实现基于binlog的主从同步
- (vue.js)axios interceptors 拦截器中添加headers 属性
- 推荐一个 Java 里面比较牛逼的公众号!