为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode1018. 可被 5 整除的二进制前缀 | Binary Prefix Divisible By 5

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10633347.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 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] is 0 or 1

给定由若干 0 和 1 组成的数组 A。我们定义 N_i:从 A[0] 到 A[i] 的第 i 个子数组被解释为一个二进制数(从最高有效位到最低有效位)。

返回布尔值列表 answer,只有当 N_i 可以被 5 整除时,答案 answer[i] 为 true,否则为 false

示例 1:

输入:[0,1,1]
输出:[true,false,false]
解释:
输入数字为 0, 01, 011;也就是十进制中的 0, 1, 3 。只有第一个数可以被 5 整除,因此 answer[0] 为真。

示例 2:

输入:[1,1,1]
输出:[false,false,false]

示例 3:

输入:[0,1,1,1,1,1]
输出:[true,false,false,false,true,false]

示例 4:

输入:[1,1,1,0,1]
输出:[false,false,false,false,false]

提示:

  1. 1 <= A.length <= 30000
  2. A[i] 为 0 或 1

Runtime: 88 ms
Memory Usage: 20.7 MB
 1 class Solution {
 2     func prefixesDivBy5(_ A: [Int]) -> [Bool] {
 3         var ret:[Bool] = [Bool]()
 4         var x:Int = 0
 5         for v in A
 6         {
 7             x = (x * 2 + v) % 5
 8             ret.append(x == 0)
 9         }
10         return ret
11     }
12 }

88ms

 1 class Solution {
 2     func prefixesDivBy5(_ A: [Int]) -> [Bool] {
 3         var curr = 0
 4         var result = A.map { _ in false }
 5         for i in 0..<A.count {
 6             curr = (curr << 1) + A[i]
 7             result[i] = (curr % 5 == 0)
 8             curr = curr % 5
 9         }
10         return result
11     }
12 }

96ms

 1 class Solution {
 2     func prefixesDivBy5(_ A: [Int]) -> [Bool] {
 3         var sum = 0
 4         var ost = Array([1,2,4,3].reversed())
 5         let shift = A.count % 4
 6         var res: [Bool] = []
 7         for (i,n) in A.enumerated() {
 8             let ind = ((i+shift) % 4)
 9             sum += (n == 1 ? ost[ind] : 0)
10             res.append((sum % 5) == 0)
11         }
12         
13         return res
14     }
15 }

104ms

 1 class Solution {
 2     func prefixesDivBy5(_ A: [Int]) -> [Bool] {
 3         var num = 0
 4         var answers = [Bool]()
 5         for a in A {
 6             num += a
 7             answers.append(num % 5 == 0)
 8             num *= 2
 9             num = num % 5
10         }
11         
12         return answers
13     }
14 }

 

posted @ 2019-03-31 21:36  为敢技术  阅读(578)  评论(0编辑  收藏  举报