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 = (t * 2 + a)%5
            ans.append(False if t else True)
        return ans

原文地址:https://www.cnblogs.com/zywscq/p/10739388.html

时间: 2024-10-22 07:09:10

Leetcode 1018. Binary Prefix Divisible By 5的相关文章

LeetCode.1018-可被5整除的二进制数(Binary Prefix Divisible By 5)

这是小川的第379次更新,第407篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第241题(顺位题号是1018).给定0和1的数组A,考虑N_i:从A[0]到A[i]的第i个子数组被解释为二进制数(从最高有效位到最低有效位). 返回布尔值answer列表,当且仅当N_i可被5整除时,answer[i]为true. 例如: 输入:[0,1,1] 输出:[true,false,false] 说明:二进制输入数字为0,01,011,转为十进制数,分别为0,1和3.只有第一

Binary Prefix Divisible By 5 LT1018

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 divi

Leetcode-1029 Binary Prefix Divisible By 5(可被 5 整除的二进制前缀)

1 class Solution 2 { 3 public: 4 vector<bool> prefixesDivBy5(vector<int>& A) 5 { 6 vector<bool> v(A.size()); 7 int rnt = 0; 8 for(int i = 0; i< A.size();i ++) 9 { 10 if(A[i]==1) 11 { 12 if(rnt==0) 13 {rnt = 1;v[i] = false;} 14 els

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space s

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

timus 1018. Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enu