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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-prxkudza-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.)git

Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.github

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:app

Input: [0,1,1,1,1,1]
Output: [true,false,false,false,true,false] 

Example 4:ide

Input: [1,1,1,0,1]
Output: [false,false,false,false,false] 

Note:spa

  1. 1 <= A.length <= 30000
  2. A[i] is 0 or 1

給定由若干 0 和 1 組成的數組 A。咱們定義 N_i:從 A[0] 到 A[i] 的第 i 個子數組被解釋爲一個二進制數(從最高有效位到最低有效位)。code

返回布爾值列表 answer,只有當 N_i 能夠被 5 整除時,答案 answer[i] 爲 true,不然爲 falsehtm

示例 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 }
相關文章
相關標籤/搜索