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

[Swift]LeetCode485.最大连续1的个数|MaxConsecutiveOnes

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

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

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

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

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

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3. 

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000

给定一个二进制数组, 计算其中最大连续1的个数。

示例 1:

输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.

注意:

  • 输入的数组只包含 0 和1
  • 输入数组的长度是正整数,且不超过 10,000。

 56ms

 1 class Solution {
 2     func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
 3         if nums.count == 1 && nums[0] == 1{
 4             return 1
 5         }
 6         var count = 0
 7         var res = 0
 8         
 9         for i in 0..<nums.count {
10            if(nums[i] == 1)  { count = count + 1 }//遇1则加
11             if(count > res)  { res = count }//判断是否大于当前最大连续值
12             if(nums[i] == 0)  { count = 0 }//遇0则置为0 
13         }
14         return res
15     }
16 }

64ms

 1 class Solution {
 2     func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
 3         var count = 0
 4         var maxcount = 0
 5         for num in nums {
 6             if num == 1 {
 7                 count = count + 1
 8                 maxcount = max(count,maxcount)
 9             } else {
10                 count = 0
11             } 
12         }
13         
14         return maxcount
15     }
16 }

76ms

 1 class Solution {
 2     func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
 3         var res = 0
 4         var currentCount = 0
 5 
 6         for num in nums {
 7             if num == 1 {
 8                 currentCount += 1
 9             } else {
10                 currentCount = 0
11             }
12             if currentCount > res {
13                 res = currentCount
14             }
15         }
16 
17         return res
18     }
19 }

80ms

 1 class Solution {
 2     func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
 3         var result: Int = 0
 4         var count: Int = 0
 5         for num in nums {
 6             if num != 1{
 7                 if count > result{
 8                     result = count
 9                 }
10                 count = 0
11             }else{
12                 count += 1
13             }
14         }
15         return max(count, result)
16     }
17 }

312ms
 1 class Solution {
 2     func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
 3         var start = -1
 4         var maxLength = 0
 5         
 6         for i in 0 ..< nums.count {
 7             switch (nums[i], start) {
 8             case (1, -1):
 9                 start = i
10             case (1, _), (0, -1):
11                 break
12             case (0, _):
13                 maxLength = max(maxLength, i - start)
14                 start = -1
15             default:
16                 break
17             }
18         }
19         
20         if start >= 0 {
21             maxLength = max(maxLength, nums.count - start)
22         }
23         
24         return maxLength
25     }
26 }

Runtime: 328 ms

Memory Usage: 19.2 MB
 1 class Solution {
 2     func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
 3         var count = 0
 4         var returnNum = 0
 5         
 6         for num in nums {
 7             count = (count + 1) * num
 8             returnNum = max(returnNum, count)
 9         }
10         
11         return returnNum       
12     }
13 }

 

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
2.6 Swift中的元组(Tuples) [Swift原创教程]发布时间:2022-07-13
下一篇:
iOS Sprite Kit最新特性Physics Field虚拟物理场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