algorithm-base/animation-simulation/数组篇/leetcode485最大连续1的个数.md

234 lines
6.5 KiB
Java
Raw Normal View History

2021-07-23 15:44:19 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
2021-03-20 08:30:29 +00:00
>
>
>
2021-07-20 13:13:10 +00:00
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 08:30:29 +00:00
2021-03-20 07:48:03 +00:00
#### [485. 1 ](https://leetcode-cn.com/problems/max-consecutive-ones/)
2021-03-17 11:49:19 +00:00
2021-07-23 15:44:19 +00:00
1
2021-03-17 11:49:19 +00:00
1:
> : [1,1,0,1,1,1]
> : 3
2021-07-23 15:44:19 +00:00
> : 1 1 3.
2021-03-17 11:49:19 +00:00
100% , Math.max()使 right 1 1
2021-07-20 13:13:10 +00:00
right - left 1 right 0 right - left + 1
2021-03-17 11:49:19 +00:00
使 while 0 1 left left = right
2021-07-29 02:33:38 +00:00
![leetcode4851](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/leetcode485最长连续1的个数.7avzcthkit80.gif)
2021-03-17 11:49:19 +00:00
Java Code:
2021-03-17 11:49:19 +00:00
```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int len = nums.length;
int left = 0, right = 0;
int maxcount = 0;
while (right < len) {
if (nums[right] == 1) {
right++;
continue;
}
//保存最大值
maxcount = Math.max(maxcount, right - left);
//跳过 0 的情况
while (right < len && nums[right] == 0) {
right++;
}
//同一起点继续遍历
left = right;
}
return Math.max(maxcount, right-left);
}
}
```
Python3 Code:
```python
from typing import List
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int])->int:
leng = len(nums)
left = 0
right = 0
maxcount = 0
while right < leng:
if nums[right] == 1:
right += 1
continue
#
maxcount = max(maxcount, right - left)
# 0
while right < leng and nums[right] == 0:
right += 1
#
left = right
return max(maxcount, right - left)
```
2021-03-17 11:49:19 +00:00
2021-07-17 04:13:15 +00:00
Swift Code
```swift
class Solution {
func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
var left = 0, right = 0, res = 0
let len = nums.count
while right < len {
if nums[right] == 1 {
right += 1
continue
}
// 保存最大值
res = max(res, right - left)
// 跳过 0 的情况
while right < len && nums[right] == 0 {
right += 1
}
// 同一起点继续遍历
left = right
}
return max(res, right - left)
}
}
```
2021-03-17 11:49:19 +00:00
1 nums[i] == 1 count++nums[i] 0 count count 1
Java Code:
2021-03-17 11:49:19 +00:00
```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int maxcount = 0;
2021-07-20 13:13:10 +00:00
2021-03-17 11:49:19 +00:00
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 1) {
count++;
//这里可以改成 while
} else {
maxcount = Math.max(maxcount,count);
count = 0;
}
}
return Math.max(count,maxcount);
}
}
```
Python3 Code:
```py
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = i = t = 0
for j in range(len(nums)):
if nums[j] == 1:
t += 1
ans = max(ans, t)
else:
i = j + 1
t = 0
return ans
```
2021-07-17 04:13:15 +00:00
Swift Code
```swift
class Solution {
func findMaxConsecutiveOnes(_ nums: [Int]) -> Int {
let len = nums.count
var maxCount = 0, count = 0
for i in 0..<len {
if nums[i] == 1 {
count += 1
} else { // 这里可以改成 while
maxCount = max(maxCount, count)
count = 0
}
2021-07-20 13:13:10 +00:00
}
2021-07-17 04:13:15 +00:00
return max(maxCount, count)
}
}
```
2021-07-20 13:13:10 +00:00
C++ Code
```C++
class Solution
{
public:
int findMaxConsecutiveOnes(vector<int> &nums)
{
int s = 0;
int e = 0;
int result = 0;
int size = nums.size();
while (s < size && e < size)
{
while (s < size && nums[s++] == 1)
{
e = s;
while (e < size && nums[e] == 1)
{
e++;
};
//注意需要加1, 可以使用极限条件测试
int r = e - s + 1;
if (r > result)
result = r;
s = e;
}
}
return result;
}
};
2021-07-23 15:44:19 +00:00
```
2021-07-27 18:26:32 +00:00
Go Code:
```go
func findMaxConsecutiveOnes(nums []int) int {
cnt, maxCnt := 0, 0
for i := 0; i < len(nums); i++ {
if nums[i] == 1 {
cnt++
} else {
maxCnt = max(maxCnt, cnt)
cnt = 0
}
}
return max(maxCnt, cnt)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
```