【leetcode】1018. Binary Prefix Divisible By 5
题目如下:
Given an array
Aof0s and1s, 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]istrueif and only ifN_iis 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 <= 30000A[i]is0or1
解题思路:本题很简单,往左移位即可。每移动一位,如果当前位置的值是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
【leetcode】1018. Binary Prefix Divisible By 5的更多相关文章
- 【LeetCode】1018. Binary Prefix Divisible By 5 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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 ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
随机推荐
- HashMap 重新学习
HashMap 重新学习 先使用 HashCode() 方法,该方法决定位置. 然后使用 equals() 方法,决定在相同位置的时候,是否覆盖. 当程序试图将一个键值对放入 HashMap 的时候, ...
- 用redlock实现redis的分布式锁
本文是一个demo,利用多进程,来模拟使用redis分布式锁的使用场景.本机需要安装redis,python3.7下运行代码.分布式锁用redlock这个包实现,实现步骤分三步: 实例化锁:rlock ...
- mypwd实现
实现mypwd 要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 步骤 查看pwd的作用man -k pwd ...
- 使div弹窗可拖拽指令
在项目开发过程中,有些情况dialog弹窗,直接使用div模拟弹窗效果,并需要支持div可拖拽. div模拟弹窗效果: (1)在用于存放指令的文件夹内,新建js文件,命名为:drag.js.具体代码如 ...
- Webhook到底是个啥?
参照: https://segmentfault.com/a/1190000015437514
- day05—JavaScript之函数调用
转行学开发,代码100天——2018-03-21 JavaScript中的函数调用有4种方式: 方式一:直接通过函数名调用 在 HTML 中默认的全局对象是 HTML 页面本身,所以函数是属于 HTM ...
- java切分查询数据库表
在实际应用中,我经常用到遇到根据单号查询,单号又是批量如1000个单号,直接1000个in子查询是不行的,子查询是用上限的.如果表中数据达到上百万以上.即使有单号字段有索引查询也是很慢.这时可以用切分 ...
- curl的一些常用命令
在学习nodejs中get到了一项新的技能crul curl 可以给在命令行上面给node服务器发送一些信息,然后得到服务器返回而响应信息,在命令行中打印出来. 下面是我整理的一些常用的命令:
- curl 和 wget 命令
1. curl curl 支持 HTTP.HTTPS.FTP 等协议,还支持 POST.cookies.认证.从指定偏移处下载部分文件.User-Agent.限速.文件大小.进度条等特征. 1.1 选 ...
- 第 1 章 前端之html
一.html初始 1.web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM ...