GVKun编程网logo

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

7

本篇文章给大家谈谈[Swift]LeetCode485.最大连续1的个数|MaxConsecutiveOnes,以及最大连续1的个数二的知识点,同时本文还将给你拓展1的最大连续数MaxConsecut

本篇文章给大家谈谈[Swift]LeetCode485. 最大连续1的个数 | Max Consecutive Ones,以及最大连续1的个数二的知识点,同时本文还将给你拓展1的最大连续数 Max Consecutive Ones、485. Max Consecutive Ones、485. Max Consecutive Ones - LeetCode、@size(max = value)和@min(value)@max(value)之间的差异等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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

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

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

Example 1:

Input: [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]
输出: 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 }

1的最大连续数 Max Consecutive Ones

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

解决:

①暴力解决:使用一个计数器,一个max用于保存最大连续数,9ms

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int max = 0;
        int count = 0;
        for (int i = 0;i < nums.length ;i ++ ) {
            if (nums[i] == 1) {
                count ++;
            }else{
                count = 0;
            }
            if (count > max) {
                max = count;
            }
        }
        return max;
    }
}

485. Max Consecutive Ones

485. 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时候归零

复杂度

时间O(n) 空间O(1)

代码

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int res = 0, count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                count++;
                res = Math.max(count, res);
            }
            else {
                count = 0;
            }
        }
        return res;
    }
}

485. Max Consecutive Ones - LeetCode

485. Max Consecutive Ones - LeetCode

Question

485. Max Consecutive Ones

Solution

题目大意:给一个数组,取连续1的最大长度

思路:遍历数组,连续1就加1,取最大

Java实现:

public int findMaxConsecutiveOnes(int[] nums) {
    if (nums == null) return 0;
    int result = 0;
    int tmp = 0;
    for (int i : nums) {
        if (i == 1) {
            tmp++;
        } else {
            result = tmp > result? tmp: result;
            tmp = 0;
        }
    }
    result = tmp > result? tmp: result;
    return result;
}

@size(max = value)和@min(value)@max(value)之间的差异

@size(max = value)和@min(value)@max(value)之间的差异

我想做一些域验证

在我的对象中,我有一个整数,

现在我的问题是我是否写

@Min(SEQ_MIN_VALUE)@Max(SEQ_MAX_VALUE)private Integer sequence;

 @Size(min = 1, max = NAME_MAX_LENGTH) private Integer sequence;

如果是整数,则哪一个适合域验证。

有人可以解释一下两者之间的区别吗?

谢谢。

答案1

小编典典

@Min@Max用于验证数字字段,其可以被String(代表数字), ,,int 等和它们各自的原始包装。short``byte

@Size 用于检查字段的长度约束。

按照文档@Size的支持StringCollectionMaparrays@Min@Max支持原语及其包装。请参阅文档。

今天关于[Swift]LeetCode485. 最大连续1的个数 | Max Consecutive Ones最大连续1的个数二的分享就到这里,希望大家有所收获,若想了解更多关于1的最大连续数 Max Consecutive Ones、485. Max Consecutive Ones、485. Max Consecutive Ones - LeetCode、@size(max = value)和@min(value)@max(value)之间的差异等相关知识,可以在本站进行查询。

本文标签: