题目如下:

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 = 0
for i in A:
val = val << 1
if i == 1:
val += 1
res.append(val % 5 == 0)
return res

最新文章

  1. 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(0)
  2. 剑指架构师系列-Hibernate需要掌握的Annotation
  3. flask中的session对象方法
  4. IIS启用.net2.0
  5. ubuntu 12.04 安装 codeblock 12.11
  6. STC-ISP下载过程
  7. (转)关于c#中的事件
  8. java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception
  9. IOC框架之一Autofac
  10. BST(Binary Search Tree)
  11. 【CSS】思考和再学习——关于CSS中浮动和定位对元素宽度/外边距/其他元素所占空间的影响
  12. Docker 初步认识
  13. JQ在线引用地址
  14. canvas图表详解系列(4):动态散点图
  15. ES6标准入门 第一章:简介
  16. org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found :
  17. GDAL指定自定义的金字塔目录
  18. 【Spring学习笔记-MVC】Spring MVC之多文件上传 (zhan)
  19. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫
  20. jquery方法.serializeArray()获取name和value并转为json数组

热门文章

  1. Spring Security 3.1 中功能强大的加密工具 PasswordEncoder
  2. POI 单元格类型CellType
  3. Jmeter从数据库中读取数据
  4. Run Your Tensorflow Deep Learning Models on Google AI
  5. JS获取当前时间并格式化
  6. OSPF协议——原理及实验
  7. spring事务——try{...}catch{...}中事务不回滚的几种处理方式(转载)
  8. Mysql 实现基于binlog的主从同步
  9. (vue.js)axios interceptors 拦截器中添加headers 属性
  10. 推荐一个 Java 里面比较牛逼的公众号!