- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
题目地址:https://leetcode.com/problems/binary-prefix-divisible-by-5/
Given an array A
of 0
s and 1
s, 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]is0or1
;
给出一个数组,判断数组的每个位置构成的前缀能不能被5整除。
这个题肯定不能蛮力求解,最简单的方法就是利用求余的性质。我们每次只用保存前缀对5的余数,在求下一个位置的时候把上一次的前缀×2 + 当前的数字
再模5.
求余的性质:
((a +b)mod p × c) mod p = ((a × c) mod p + (b × c) mod p) mod p
(a×b) mod c=((a mod c) * (b mod c)) mod c
(a+b) mod c=((a mod c)+ (b mod c)) mod c
(a-b) mod c=((a mod c)- (b mod c)) mod c
所以,a扩大x倍之后模一个数字,等于((a % 5) * (x % 5)) % 5.
Python代码如下:
class Solution(object):
def prefixesDivBy5(self, A):
"""
:type A: List[int]
:rtype: List[bool]
"""
res = []
prefix = 0
for a in A:
prefix = (prefix * 2 + a) % 5
res.append(prefix == 0)
return res
1 2 3 4 5 6 7 8 9 10 11 12
2022
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发
我想知道这在 SQL 中是否可行。假设您有两个表 A 和 B,并且您对表 A 进行选择并在表 B 上进行联接: SELECT a.*, b.* FROM TABLE_A a JOIN TABLE_B
我认为这只是一个一般的 C++ 问题: 我正在尝试使用 gnu c++ 编译器在 Linux Fedora 上编译本地版本的 ffmpeg。我的源代码位于以下一堆文件夹中: ~//Downloads/
我听说在 Linux 上编译 PHP 时设置 --prefix=PREFIX 选项将允许您一次安装多个 PHP 而不会发生冲突。 (我认为如果未设置默认值是 /usr/local)。但是,我不确定它到
我有以下 conda 环境文件 environment.yml: name: testproject channels: - defaults dependencies: - python=3.7 p
目前在我的 Makefile 中我有: prefix ?= /usr/local 这样我就可以在调用 make 时覆盖 prefix 值,如下所示: make prefix="/new_path" 我
我正在尝试创建一个计算 pptx 中单词的函数文档。问题是我不知道如何只找到这种标签: Some Text 当我尝试:print xmlTree.findall('.//a:t') , 它返回 Syn
gitconfig(1) : If not set explicitly with --file, there are four files where git config will search
嗨,我是 Spring MVC 的新手,我正在关注 Spring 引用文档,我对 View 解析器有疑问。这是我的示例代码。 @Controller @RequestMapping("/form")
我一般都是通过pip安装python包的。 对于 Google App Engine,我需要将包安装到另一个目标目录。 我试过了: pip install -I flask-restful --tar
我对一些使用指令 ngb-pagination 的组件进行了 Angular 测试。来自 ng-bootstrap . 现在,在我的测试中,我模拟这个组件如下: // on next line I g
我无法运行npm install npm@latest -g,而不会遇到权限错误并被迫使用sudo npm install npm@latest -g。我可以在不使用 sudo 的情况下运行它,但是
假设我有一个字符串公式,它的格式是:“func(a+b,c)”,其中func是一个定制函数,这个字符串同时包含中缀(即+)和前缀(即func)表示,我想将其转换为具有所有前缀表示的字符串,“func(
我已经写下了一个简单的函数来确定 str1 是否是 str2 的前缀。这是一个非常简单的函数,看起来像这样(在 JS 中): function isPrefix(str1, str2) // dete
我们有 git remote add origin http://...避免重复输入实际的源代码库路径。但是如何git subtree --prefix=... ?每次拉/推子树内容时,很难跟踪、记住
我的 travis 工作遇到了以下问题: Process Output:Could not find platform independent libraries Process Output:Co
我正在阅读 Artifice 的来源并看到: module Artifice NET_HTTP = ::Net::HTTP # ... end 行:https://github.com/wyc
我正在阅读有关 Angular 路由的文档并创建了一个简单的测试: const routes: Route[] = [ { path: '', redirectTo: '/home', pat
所以使用 Material 我们有一些代码来问一个问题,我们想在输入的前面添加一个 $ 符号或使用占位符。在这个垫子字段内部,我们还有一个垫子标签。如果我使用 matprefix,它会将美元符号放在标
目前我希望为 Apache Mesos 编写一个脚本来启动主/从(在 2 个不同的节点上)。 我有引用http://mesos.apache.org/documentation/latest/depl
我已为文档建立索引,每个文档都有一个字段:“CodeName”,其值类似于以下内容: document 1 has CodeName: "AAA01" document 2 has CodeName:
我是一名优秀的程序员,十分优秀!