• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode930.和相同的二元子数组|BinarySubarraysWithSum

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

In an array A of 0s and 1s, how many non-empty subarrays have sum S?

 Example 1:

Input: A = 2
Output: 4
Explanation: 
The 4 subarrays are bolded below:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]

 Note:

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

在由若干 0 和 1  组成的数组 A 中,有多少个和为 S 的非空子数组。

 示例:

输入:A = [1,0,1,0,1], S = 2
输出:4
解释:
如下面黑体所示,有 4 个满足题目要求的子数组:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]

 提示:

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

220ms

 1 class Solution {
 2     func numSubarraysWithSum(_ A: [Int], _ S: Int) -> Int {
 3         var n:Int = A.count
 4         var cum:[Int] = [Int](repeating: 0,count: n + 1)
 5         for i in 0..<n
 6         {
 7             cum[i + 1] = cum[i] + A[i]
 8         }
 9         
10         var ret:Int = 0
11         var f:[Int] = [Int](repeating: 0,count: 30003)
12         for i in 0...n
13         {
14             if cum[i] - S >= 0
15             {
16                 ret += f[cum[i] - S]
17             }
18             f[cum[i]] += 1
19         }
20         return ret
21     }
22 }

232ms

 1 class Solution {
 2     func numSubarraysWithSum(_ A: [Int], _ S: Int) -> Int {
 3         
 4         var count = 0
 5         var zCount = 0
 6         var prevVal = 0
 7         if S == 0 {
 8             for val in A {
 9                 if val == 0 {
10                     print(zCount)
11                     prevVal += 1
12                     zCount += prevVal
13                 } else {
14                     count += zCount
15                     zCount = 0
16                     prevVal = 0
17                 }
18             }
19             count += zCount
20             return count
21         }
22         
23         var leftZeros:[Int] = Array(repeating: 0, count: A.count)
24         var rightZeros:[Int] = Array(repeating: 0, count: A.count)
25         var onesInd:[Int] = []
26         
27         
28         for i in 0..<A.count {
29             if A[i] == 0 {
30                 count += 1
31             } else {
32             leftZeros[i] = count
33             onesInd.append(i)
34             count = 0
35             }
36         }
37         
38         count = 0
39         for i in (0..<A.count).reversed() {
40             if A[i] == 0 {
41                 count += 1
42             }else {
43             rightZeros[i] = count
44             count = 0
45             }
46         }
47         
48         if onesInd.count < S {
49             return 0
50         }
51         
52         count = 0
53         for i in 0...(onesInd.count - S) {
54             let leftInd = onesInd[i]
55             let rightInd = onesInd[i + S - 1]
56             let subCount = (1 + leftZeros[leftInd]) * (rightZeros[rightInd] + 1)
57             count += subCount
58         }
59         return count
60     }
61 }

304ms

 1 class Solution {
 2     func numSubarraysWithSum(_ A: [Int], _ S: Int) -> Int {
 3         var counter = 0
 4         var dp = [Int](repeating: 0, count: A.count + 1)
 5         for i in 0..<A.count {
 6             counter += A[i]
 7             dp[i + 1] = counter
 8         }
 9         var result = 0
10         var dict = [Int : Int]()
11         dict[0] = 1
12         for i in 1..<dp.count {
13             let a = dp[i]
14             if let value = dict[a - S] {
15                 result += value
16             }
17             dict[a] = dict[a] ?? 0
18             dict[a] = dict[a]! + 1
19         }
20         return result 
21     }
22 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
iOS:学习笔记,Swift名字空间发布时间:2022-07-13
下一篇:
oc和swift混编(方法相互调用)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap