【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. ...
随机推荐
- flask中获取request的参数的方法
request请求总体分为两类: 1.get请求 访问时会在地址栏直接显示参数不安全,且参数大小比较小. 2.post请求 参数不显示在地址栏,一般用户注册.登录都通过post请求完成. flask获 ...
- PHP不使用内置函数intval(),实现字符串转整数
平时我们用PHP时,将字符串转化为整型时,一般都是使用 intval() 内置函数,那么如果我们自己写,怎么写一个呢? 此时我们可以利用 ASCII 码计算得整数的特性,因为每个字符都对应一个 ASC ...
- Gym 100917F Find the Length
题目链接:http://codeforces.com/gym/100917/problem/F ---------------------------------------------------- ...
- CDN:BootCDN
ylbtech-CDN:BootCDN BootCDN稳定.快速.免费的前端开源项目 CDN 加速服务共收录了 3351 个前端开源项目 1. 推荐返回顶部 1. bootstrap Bootstra ...
- leetcode 190. 颠倒二进制位(c++)
颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100输出: 00111001011110000010100101000000 ...
- css样式表的理解
全拼Cascading Style Sheete 美化html网页 1分为 内联样式表 和html联合显示 内嵌样式表 在单独区域内嵌,必须在head 外部样式表 需建一个css文件,保存并附加 2选 ...
- MVC终极解释
之前校招笔面试老师被问起MVC,虽然都知道怎么回事.但每次组织语言总觉得答得的简洁明了和突出重点.下面是我总结,希望未来找工作的学弟学妹们能不在受此大路边上的问题困扰. M-V-C 即Model-Vi ...
- 关于Vue+iview的前端简单的导入数据(excel)
前一段时间项目经历了纯前端处理导入excel文件并处理等问题,数据量大的时候时间上长的一比,三千条数据需要三四秒甚至更长,不管产品咋想的,具体做法为: 首先下载一个这玩意: 进行简单封装一下: < ...
- go tour - Go 入门实验教程
在线实验地址 - 官网 在线实验地址 - 国内 可以将官方教程作为独立程序在本地安装使用,这样无需访问互联网就能运行,且速度更快,因为是在你的机器上构建并运行代码示例. 本地运行此教程的中文版的步骤如 ...
- HTML--JS 二级联动
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...