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                     else if(rnt==1)
15                         {rnt = 3;v[i] = false;}
16                     else if(rnt==2)
17                         {rnt = 0;v[i] = true;}
18                     else if(rnt==3)
19                         {rnt = 2;v[i] = false;}
20                     else if(rnt==4)
21                         {rnt = 4;v[i] = false;}
22                 }
23                 else if(A[i]==0)
24                 {
25                     if(rnt==0)
26                         {rnt = 0;v[i] = true;}
27                     else if(rnt==1)
28                         {rnt = 2;v[i] = false;}
29                     else if(rnt==2)
30                         {rnt = 4;v[i] = false;}
31                     else if(rnt==3)
32                         {rnt = 1;v[i] = false;}
33                     else if(rnt==4)
34                         {rnt = 3;v[i] = false;}
35                 }
36             }
37             return v;
38         }
39 };

原文地址:https://www.cnblogs.com/Asurudo/p/10630798.html

时间: 2024-10-25 05:53:22

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

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

Leetcode 1029. 可被 5 整除的二进制前缀

1029. 可被 5 整除的二进制前缀 显示英文描述 我的提交返回竞赛 用户通过次数467 用户尝试次数662 通过次数477 提交次数1964 题目难度Easy 给定由若干 0 和 1 组成的数组 A.我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位). 返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false. 示例 1: 输入:[0,1,1] 输出:[true,

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 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? 分析:求二叉树的中序