牛客真题(3)-交错01串

今天继续刷牛客真题,给定一个01字符串,判断其中是交错01串的子字符串的最大长度。

分析:
通过设置两个变量,一个变量统计该位置的满足01串的子字符串的长度,另一个变量为最大长度,通过一次遍历得到结果。

问题:
1、字符串的输入;
2、调用较大函数max;

附上C++代码:

#include
#include
using namespace std;

int main()
{
    string s;
    getline(cin,s);
    int Len=1;
    int maxLen=1;
    for(int i=1;i

python实现:
分析:
设置一个列表,存放连续的01串长度,这样在即使是最后一个字符也会判断进去,最后输出列表中最大的值

附上python代码:

a=input()
tmp=1
Len=[]
for i in range(len(a)-1):
    if a[i+1]!=a[i]:
        tmp+=1
        Len.append(tmp)
    else:
        tmp=1
if len(Len)==0:
    print(1)
else:
    print(max(Len))

你可能感兴趣的:(代码训练)