【leetcode】1018. Binary Prefix Divisible By 5
题目如下:
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
【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. ...
随机推荐
- Spring源码解读--(一)源码下载
走在Java程序员这条路上,网上Java各种工具满天飞,写个简单的CRUD,相信是个开发都能写出来,于是在思考如何可以在同行业中更有竞争力(其实就是如何赚更多钱).那么,老大给我推荐了Spring源码 ...
- 关于TCP\IP协议的简单理解
一: HTTP是基于TCP协议的,TCP属于传输层.HTTP属于应用层. 另一个属于传输层的是UDP协议. 但HTTP是基于TCP这个协议,不是UDP这个协议. TCP相比于UDP,更安全,稳定.因为 ...
- 1207D Number Of Permutations
题目大意 给你n个二元组 问你有几种排列是的按两个关键字中的任意一个都不是不降排列的 分析 不妨容斥 我们先加上总的方案数$n!$ 之后我们按第一个关键字排序 因为值相同的情况下不影响答案 所以让总方 ...
- 测开之路六十二:接口测试平台之公共的js、html、平台入口
common.js //定义后台的host和端口var host = 'http://192.168.xxx.1:8000'; //'http://127.0.0.1:8000'; //用于发送htt ...
- Vim 8.0 版本安装方法及添加Python支持
利用Git安装 最简单也是最有效的方法 1. 获取Vim仓库: git clone https://github.com/vim/vim.git 2. 升级到最新的版本: cd vim git pul ...
- 机器学习实战笔记-5-Logistic回归
Logistic回归 优缺点 适用范围 优点:计算代价不高,易于理解和实现. 缺点:容易欠拟合,分类精度可能不高. 适用于:数值型和标称型数据. 仅用于二分类 原理: 每个特征都乘以一个回归系数> ...
- Naive Bayes Algorithm And Laplace Smoothing
朴素贝叶斯算法(Naive Bayes)适用于在Training Set中,输入X和输出Y都是离散型的情况.如果输入X为连续,输出Y为离散,我们考虑使用逻辑回归(Logistic Regression ...
- div在上一层容器居中的方法
今天又学了一种新方法,其实不算新,只是我不知道而已,想想学了一年多,现在什么动效都能写点出来了,但是一些基础的东西还是掌握不好,所以我现在依然会一遍遍的复习h5和css3,这就是自学的坏处,不知道的东 ...
- P1613跑路
题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资,小A买了一个十分牛B的空间跑路器,每秒钟 ...
- Mybatis-技术专区-Mapper接口以及Example的实例函数及详解
一.mapper接口中的方法解析 mapper接口中的函数及方法 int countByExample(UserExample example) thorws SQLException 按条件 ...