Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19b4c74

Browse files
committed
commit solution 485
1 parent 3b48480 commit 19b4c74

File tree

7 files changed

+94
-4
lines changed

7 files changed

+94
-4
lines changed

‎index-tags.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| [268](https://leetcode-cn.com/problems/missing-number) | [缺失数字](/solution/200-299/0268.missing-number/) | `位运算`,`数组`,`数学` | <font color=green>简单</font> ||
2222
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
2323
| [414](https://leetcode-cn.com/problems/third-maximum-number) | [第三大的数](/solution/400-499/0414.third-maximum-number/) | `数组` | <font color=green>简单</font> ||
24+
| [485](https://leetcode-cn.com/problems/max-consecutive-ones) | [最大连续1的个数](/solution/400-499/0485.max-consecutive-ones/) | `数组` | <font color=green>简单</font> ||
2425

2526
#### **字符串**
2627

@@ -29,6 +30,7 @@
2930
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
3031
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
3132
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
33+
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> ||
3234
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
3335
| [344](https://leetcode-cn.com/problems/reverse-string) | [反转字符串](/solution/300-399/0344.reverse-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||
3436
| [345](https://leetcode-cn.com/problems/reverse-vowels-of-a-string) | [反转字符串中的元音字母](/solution/300-399/0345.reverse-vowels-of-a-string/) | `双指针`,`字符串` | <font color=green>简单</font> ||

‎index-type.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
2121
| [38](https://leetcode-cn.com/problems/count-and-say) | [外观数列](/solution/1-99/0038.count-and-say/) | `字符串` | <font color=green>简单</font> |
2222
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> |
23-
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> |
23+
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> ||
2424
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
2525
| [67](https://leetcode-cn.com/problems/add-binary) | [二进制求和](/solution/1-99/0067.add-binary/) | `数学`,`字符串` | <font color=green>简单</font> |
2626
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> |
@@ -136,7 +136,7 @@
136136
| [475](https://leetcode-cn.com/problems/heaters) | [供暖器](/solution/400-499/0475.heaters/) | `二分查找` | <font color=green>简单</font> |
137137
| [476](https://leetcode-cn.com/problems/number-complement) | [数字的补数](/solution/400-499/0476.number-complement/) | `位运算` | <font color=green>简单</font> |
138138
| [482](https://leetcode-cn.com/problems/license-key-formatting) | [密钥格式化](/solution/400-499/0482.license-key-formatting/) | | <font color=green>简单</font> |
139-
| [485](https://leetcode-cn.com/problems/max-consecutive-ones) | [最大连续1的个数](/solution/400-499/0485.max-consecutive-ones/) | `数组` | <font color=green>简单</font> |
139+
| [485](https://leetcode-cn.com/problems/max-consecutive-ones) | [最大连续1的个数](/solution/400-499/0485.max-consecutive-ones/) | `数组` | <font color=green>简单</font> ||
140140
| [492](https://leetcode-cn.com/problems/construct-the-rectangle) | [构造矩形](/solution/400-499/0492.construct-the-rectangle/) | | <font color=green>简单</font> |
141141
| [496](https://leetcode-cn.com/problems/next-greater-element-i) | [下一个更大元素 i](/solution/400-499/0496.next-greater-element-i/) | `` | <font color=green>简单</font> |
142142
| [500](https://leetcode-cn.com/problems/keyboard-row) | [键盘行](/solution/500-599/0500.keyboard-row/) | `哈希表` | <font color=green>简单</font> |

‎solution/1-99/_sidebar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
- [55. 跳跃游戏](solution/1-99/0055.jump-game/)
6464
- [56. 合并区间](solution/1-99/0056.merge-intervals/)
6565
- [57. 插入区间](solution/1-99/0057.insert-interval/)
66-
- [58. 最后一个单词的长度](solution/1-99/0058.length-of-last-word/)
66+
- [58. 最后一个单词的长度](solution/1-99/0058.length-of-last-word/)
6767
- [59. 螺旋矩阵 ii](solution/1-99/0059.spiral-matrix-ii/)
6868
- [60. 第k个排列](solution/1-99/0060.permutation-sequence/)
6969
- [61. 旋转链表](solution/1-99/0061.rotate-list/)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [485. 最大连续1的个数](https://leetcode-cn.com/problems/max-consecutive-ones/description/)
2+
3+
### 题目描述
4+
5+
<p>给定一个二进制数组, 计算其中最大连续1的个数。</p>
6+
7+
<p><strong>示例 1:</strong></p>
8+
9+
<pre>
10+
<strong>输入:</strong> [1,1,0,1,1,1]
11+
<strong>输出:</strong> 3
12+
<strong>解释:</strong> 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
13+
</pre>
14+
15+
<p><strong>注意:</strong></p>
16+
17+
<ul>
18+
<li>输入的数组只包含&nbsp;<code>0</code> 和<code>1</code>。</li>
19+
<li>输入数组的长度是正整数,且不超过 10,000。</li>
20+
</ul>
21+
22+
23+
24+
### 解题思路
25+
26+
![](http://lc-photo.xwlin.com/485.gif)
27+
28+
### 具体解法
29+
30+
#### **Golang**
31+
```go
32+
func findMaxConsecutiveOnes(nums []int) int {
33+
p, q := 0, 0
34+
for i := range nums {
35+
if nums[i] == 1 {
36+
p++
37+
if q < p {
38+
q = p
39+
}
40+
} else {
41+
p = 0
42+
}
43+
}
44+
return q
45+
}
46+
```
47+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=485 lang=golang
5+
*
6+
* [485] 最大连续1的个数
7+
*/
8+
9+
// @lc code=start
10+
func findMaxConsecutiveOnes(nums []int) int {
11+
p, q := 0, 0
12+
for i := range nums {
13+
if nums[i] == 1 {
14+
p++
15+
if q < p {
16+
q = p
17+
}
18+
} else {
19+
p = 0
20+
}
21+
}
22+
return q
23+
}
24+
25+
// @lc code=end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFindMaxConsecutiveOnes(t *testing.T) {
8+
var ret int
9+
var nums []int
10+
11+
nums = []int{1, 1, 0, 1, 1, 1}
12+
ret = 3
13+
if ret != findMaxConsecutiveOnes(nums) {
14+
t.Fatalf("case fails %v\n", ret)
15+
}
16+
}

‎solution/400-499/_sidebar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
- [482. 密钥格式化](solution/400-499/0482.license-key-formatting/)
9292
- [483. 最小好进制](solution/400-499/0483.smallest-good-base/)
9393
- [484. 寻找排列](solution/400-499/0484.find-permutation/)
94-
- [485. 最大连续1的个数](solution/400-499/0485.max-consecutive-ones/)
94+
- [485. 最大连续1的个数](solution/400-499/0485.max-consecutive-ones/)
9595
- [486. 预测赢家](solution/400-499/0486.predict-the-winner/)
9696
- [487. 最大连续1的个数 ii](solution/400-499/0487.max-consecutive-ones-ii/)
9797
- [488. 祖玛游戏](solution/400-499/0488.zuma-game/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /