[update] #3 数组篇添加C++ Code

This commit is contained in:
caibingcheng
2021-07-20 21:13:10 +08:00
parent 10a7420457
commit 664b801ca9
5 changed files with 215 additions and 30 deletions

View File

@@ -2,7 +2,7 @@
>
> 感谢支持该仓库会一直维护希望对各位有一丢丢帮助
>
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [485. 最大连续 1 的个数](https://leetcode-cn.com/problems/max-consecutive-ones/)
@@ -16,7 +16,7 @@
我的这个方法比较奇怪但是效率还可以战胜了 100% , 尽量减少了 Math.max()的使用我们来看一下具体思路利用 right 指针进行探路如果遇到 1 则继续走遇到零时则停下求当前 1 的个数
这时我们可以通过 right - left 得到 1 个数因为此时我们的 right 指针指在 0 所以不需要和之前一样通过 right - left + 1 获得窗口长度
这时我们可以通过 right - left 得到 1 个数因为此时我们的 right 指针指在 0 所以不需要和之前一样通过 right - left + 1 获得窗口长度
然后我们再使用 while 循环遍历完为 0 的情况跳到下一段为 1 的情况然后移动 left 指针 left = right站在同一起点继续执行上诉过程
@@ -125,7 +125,7 @@ class Solution {
int count = 0;
int maxcount = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 1) {
count++;
@@ -172,8 +172,43 @@ class Solution {
maxCount = max(maxCount, count)
count = 0
}
}
}
return max(maxCount, count)
}
}
```
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;
}
};
```