mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-12 12:51:10 +00:00
添加了python代码(数组篇)
为数组篇文件夹下的代码增加了python语言版本
This commit is contained in:
@@ -30,6 +30,8 @@
|
||||
|
||||
下面我们直接看代码吧
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int findMaxConsecutiveOnes(int[] nums) {
|
||||
@@ -58,6 +60,29 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
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)
|
||||
```
|
||||
|
||||
|
||||
刚才的效率虽然相对高一些,但是代码不够优美,欢迎各位改进,下面我们说一下另外一种情况,一个特别容易理解的方法。
|
||||
|
||||
Reference in New Issue
Block a user