在本文中,您将会了解到关于[SwiftWeeklyContest126]LeetCode1004.最大连续1的个数III|MaxConsecutiveOnesIII的新资讯,并给出一些关于1的最大连续
在本文中,您将会了解到关于[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III的新资讯,并给出一些关于1的最大连续数 Max Consecutive Ones、485. Max Consecutive Ones、485. Max Consecutive Ones - LeetCode、LeetCode - Easy - 485. Max Consecutive Ones的实用技巧。
本文目录一览:- [Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III
- 1的最大连续数 Max Consecutive Ones
- 485. Max Consecutive Ones
- 485. Max Consecutive Ones - LeetCode
- LeetCode - Easy - 485. Max Consecutive Ones
[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III
Given an array A
of 0s and 1s,we may change up to K
values from 0 to 1.
Return the length of the longest (contiguous) subarray that contains only 1s.
Example 1:
Input: A = [1,1,0],K = 2 Output: 6 Explanation: [1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:
Input: A = [0,1],K = 3 Output: 10 Explanation: [0,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Note:
1 <= A.length <= 20000
0 <= K <= A.length
-
A[i]
is0
or1
给定一个由若干 0
和 1
组成的数组 A
,我们最多可以将 K
个值从 0 变成 1 。
返回仅包含 1 的最长(连续)子数组的长度。
示例 1:
输入:A = [1,K = 2 输出:6 解释: [1,1] 粗体数字从 0 翻转到 1,最长的子数组长度为 6。
示例 2:
输入:A = [0,K = 3 输出:10 解释: [0,1] 粗体数字从 0 翻转到 1,最长的子数组长度为 10。
提示:
1 <= A.length <= 20000
0 <= K <= A.length
-
A[i]
为0
或1
1 class Solution { 2 func longestOnes(_ A: [Int],_ K: Int) -> Int { 3 var res:Int = 0 4 var zero:Int = 0 5 var left:Int = 0 6 for right in 0..<A.count 7 { 8 if A[right] == 0 9 { 10 zero += 1 11 } 12 while (zero > K) 13 { 14 if A[left] == 0 15 { 16 zero -= 1 17 } 18 left += 1 19 } 20 res = max(res,right - left + 1) 21 } 22 return res 23 } 24 }
Runtime: 576 ms
1 class Solution { 2 func longestOnes(_ A: [Int],_ K: Int) -> Int { 3 var n:Int = A.count 4 var pre:[Int] = [Int](repeating:0,count:n) 5 for i in 0..<n 6 { 7 if A[i] == 0 8 { 9 pre[i] = 1 10 } 11 } 12 for i in 1..<n 13 { 14 pre[i] = pre[i - 1] + pre[i] 15 } 16 var fans:Int = 0 17 for i in -1..<(n - 1) 18 { 19 var lo:Int = i + 1 20 var hi:Int = n - 1 21 var ans:Int = i 22 while(lo <= hi) 23 { 24 var mid:Int = (lo + hi) / 2 25 var val:Int = pre[mid] 26 if i >= 0 27 { 28 val -= pre[i] 29 } 30 if val <= K 31 { 32 ans = mid 33 lo = mid + 1 34 } 35 else 36 { 37 hi = mid - 1 38 } 39 } 40 fans = max(fans,ans - i) 41 } 42 return fans 43 } 44 }
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
and1
. - 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
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
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;
}
LeetCode - Easy - 485. Max Consecutive Ones
Topic
- Array
Description
https://leetcode.com/problems/max-consecutive-ones/
Given a binary array nums, return the maximum number of consecutive 1''s in the array.
Example 1:
Input: nums = [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.
Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 2
Constraints:
- $1 <= nums.length <= 10^5$
- nums[i] is either 0 or 1.
Analysis
略
Submission
public class MaxConsecutiveOnes {
// 我写的
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0, count = 0;
for (int num : nums) {
if (num == 0) {
count = 0;
} else {
if (++count > max) {
max = count;
}
}
}
return max;
}
// 别人的
public int findMaxConsecutiveOnes2(int[] nums) {
int maxHere = 0, max = 0;
for (int n : nums)
max = Math.max(max, maxHere = n == 0 ? 0 : maxHere + 1);
return max;
}
}
Test
import static org.junit.Assert.*;
import org.junit.Test;
public class MaxConsecutiveOnesTest {
@Test
public void test() {
MaxConsecutiveOnes obj = new MaxConsecutiveOnes();
assertEquals(3, obj.findMaxConsecutiveOnes(new int[] {1, 1, 0, 1, 1, 1}));
assertEquals(2, obj.findMaxConsecutiveOnes(new int[] {1, 0, 1, 1, 0, 1}));
assertEquals(3, obj.findMaxConsecutiveOnes2(new int[] {1, 1, 0, 1, 1, 1}));
assertEquals(2, obj.findMaxConsecutiveOnes2(new int[] {1, 0, 1, 1, 0, 1}));
}
}
关于[Swift Weekly Contest 126]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于1的最大连续数 Max Consecutive Ones、485. Max Consecutive Ones、485. Max Consecutive Ones - LeetCode、LeetCode - Easy - 485. Max Consecutive Ones的相关信息,请在本站寻找。
本文标签: