Skip to content

feat: add solutions to lc problem: No.0485 #3247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 58 additions & 56 deletions solution/0400-0499/0485.Max Consecutive Ones/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ tags:

### 方法一:一次遍历

遍历数组,记录当前连续 $1$ 的个数 `cnt`,以及最大连续 $1$ 的个数 `ans`。如果当前元素为 $1$,则 `cnt++`,否则更新 `ans`,并且 `cnt=0`。最后返回 `max(ans, cnt)` 即可
我们可以遍历数组,用一个变量 $\textit{cnt}$ 记录当前连续的 1 的个数,用另一个变量 $\textit{ans}$ 记录最大连续 1 的个数

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
当遍历到一个 1 时,将 $\textit{cnt}$ 加一,然后更新 $\textit{ans}$ 的值为 $\textit{cnt}$ 和 $\textit{ans}$ 本身的最大值,即 $\textit{ans} = \max(\textit{ans}, \textit{cnt})$。否则,将 $\textit{cnt}$ 重置为 0。

遍历结束后,返回 $\textit{ans}$ 的值即可。

时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -63,31 +67,30 @@ tags:
```python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
cnt = ans = 0
for v in nums:
if v == 1:
ans = cnt = 0
for x in nums:
if x:
cnt += 1
else:
ans = max(ans, cnt)
else:
cnt = 0
return max(ans, cnt)
return ans
```

#### Java

```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int cnt = 0, ans = 0;
for (int v : nums) {
if (v == 1) {
++cnt;
int ans = 0, cnt = 0;
for (int x : nums) {
if (x == 1) {
ans = Math.max(ans, ++cnt);
} else {
ans = Math.max(ans, cnt);
cnt = 0;
}
}
return Math.max(cnt, ans);
return ans;
}
}
```
Expand All @@ -98,52 +101,49 @@ class Solution {
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int cnt = 0, ans = 0;
for (int v : nums) {
if (v == 1) {
++cnt;
int ans = 0, cnt = 0;
for (int x : nums) {
if (x) {
ans = max(ans, ++cnt);
} else {
ans = max(ans, cnt);
cnt = 0;
}
}
return max(ans, cnt);
return ans;
}
};
```

#### Go

```go
func findMaxConsecutiveOnes(nums []int) int {
ans, cnt := 0, 0
for _, v := range nums {
if v == 1 {
func findMaxConsecutiveOnes(nums []int) (ans int) {
cnt := 0
for _, x := range nums {
if x == 1 {
cnt++
} else {
ans = max(ans, cnt)
} else {
cnt = 0
}
}
return max(ans, cnt)
return
}
```

#### TypeScript

```ts
function findMaxConsecutiveOnes(nums: number[]): number {
let res = 0;
let count = 0;
for (const num of nums) {
if (num === 0) {
res = Math.max(res, count);
count = 0;
let [ans, cnt] = [0, 0];
for (const x of nums) {
if (x) {
ans = Math.max(ans, ++cnt);
} else {
count++;
cnt = 0;
}
}
return Math.max(res, count);
return ans;
}
```

Expand All @@ -152,17 +152,19 @@ function findMaxConsecutiveOnes(nums: number[]): number {
```rust
impl Solution {
pub fn find_max_consecutive_ones(nums: Vec<i32>) -> i32 {
let mut res = 0;
let mut count = 0;
for num in nums {
if num == 0 {
res = res.max(count);
count = 0;
let mut ans = 0;
let mut cnt = 0;

for &x in nums.iter() {
if x == 1 {
cnt += 1;
ans = ans.max(cnt);
} else {
count += 1;
cnt = 0;
}
}
res.max(count)

ans
}
}
```
Expand All @@ -175,17 +177,15 @@ impl Solution {
* @return {number}
*/
var findMaxConsecutiveOnes = function (nums) {
let res = 0,
t = 0;
for (let num of nums) {
if (num == 1) {
++t;
let [ans, cnt] = [0, 0];
for (const x of nums) {
if (x) {
ans = Math.max(ans, ++cnt);
} else {
res = Math.max(res, t);
t = 0;
cnt = 0;
}
}
return Math.max(res, t);
return ans;
};
```

Expand All @@ -198,16 +198,18 @@ class Solution {
* @return Integer
*/
function findMaxConsecutiveOnes($nums) {
$tmp = $max = 0;
for ($i = 0; $i < count($nums); $i++) {
if ($nums[$i] == 1) {
$tmp++;
$ans = $cnt = 0;

foreach ($nums as $x) {
if ($x == 1) {
$cnt += 1;
$ans = max($ans, $cnt);
} else {
$max = max($tmp, $max);
$tmp = 0;
$cnt = 0;
}
}
return max($tmp, $max);

return $ans;
}
}
```
Expand Down
Loading
Loading