[Swift]LeetCode485. 最大連續1的個數 | Max Consecutive Ones

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

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

Example 1:github

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

輸入: [1,1,0,1,1,1]
輸出: 3
解釋: 開頭的兩位和最後的三位都是連續1,因此最大連續1的個數是 3.

注意:spa

  • 輸入的數組只包含 0 和1
  • 輸入數組的長度是正整數,且不超過 10,000。

 56mscode

 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 }

64mshtm

 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 }

76msblog

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