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

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 }

原文地址:https://www.cnblogs.com/strengthen/p/10465918.html

时间: 2024-10-29 23:12:28

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

485. 找出二进制串中连续的1的个数 Max Consecutive Ones

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

485 Max Consecutive Ones 最大连续1的个数

给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意:    输入的数组只包含 0 和1.    输入数组的长度是正整数,且不超过 10,000.详见:https://leetcode.com/problems/max-consecutive-ones/description/ C++: class Solution { public: int findMaxConsecu

485. Max Consecutive Ones最长的连续1的个数

[抄题]: 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. [

LeetCode(485. 最大连续1的个数)

问题描述 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1. 输入数组的长度是正整数,且不超过 10,000. 解决方案 class Solution: def findMaxConsecutiveOnes(self, nums): """ :type nums: List[int] :rtype: int &

leetcode 最大连续1的个数 III

1004. 最大连续1的个数 III 给定一个由若干 0 和 1 组成的数组 A,我们最多可以将 K 个值从 0 变成 1 . 返回仅包含 1 的最长(连续)子数组的长度. 示例 1: 输入:A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 输出:6 解释: [1,1,1,0,0,1,1,1,1,1,1] 粗体数字从 0 翻转到 1,最长的子数组长度为 6. 示例 2: 输入:A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3

最大连续1的个数

题目描述 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1. 输入数组的长度是正整数,且不超过 10,000. 分析 这道题让我们求最大连续1的个数,不是一道难题.我们可以遍历一遍数组,用一个计数器cnt来统计1的个数,方法是如果当前数字为0,那么cnt重置为0,如果不是0,cnt自增1,然后每次更新结果res即可. 贴出代码 c

485-最大连续1的个数

485-最大连续1的个数 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1. 输入数组的长度是正整数,且不超过 10,000. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/max-consecutive-ones 著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请

Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

统计数列中是连续数的个数

数据库环境:SQL SERVER2008R2 今天在群里看到一位网友提的需求,要求统计一组数据中前3位是连续数的个数,具体看贴图. 实现这个需求蛮简单的,说下我的思路: 取前3位,然后减去123,再和111求余,如果结果是0,说明这3位数连续. 是不是很简单呀,下面直接上代码. /*数据准备*/ WITH x0 AS ( SELECT 1234 AS tn UNION ALL SELECT 1235 AS tn UNION ALL SELECT 1236 AS tn UNION ALL SELE