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

[Swift]LeetCode485. 最大连续1的个数 | Max Consecutive Ones

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

 

 

posted @ 2019-03-03 15:37  为敢技术  阅读(260)  评论(0编辑  收藏  举报