携程第四题,不清楚是不是最简洁
int main() {
string s;
cin>>s;
long ans=0;
int t=0;
for (char c : s) {
if (c=='0') {
t++;
ans+=t;
} else {
t--;
if (t<0) t=0;
ans+=t;
}
}
cout<<ans<<endl;
}
string s;
cin>>s;
long ans=0;
int t=0;
for (char c : s) {
if (c=='0') {
t++;
ans+=t;
} else {
t--;
if (t<0) t=0;
ans+=t;
}
}
cout<<ans<<endl;
}
全部评论
是的 o(n)复杂度
大佬,能不能解释一下啊😂
这是基础C语言,比我司面试题还水。
相关推荐
smile丶snow:尽量保证的一张a4纸吧。为什么工作经历只有公司?如果项目经历是工作里面的,应该写到工作经历里的吧。没有写项目职责,项目全是一个人写的吗?标题大小也好怪。两个项目的分割看不出来是,它和那些项目成功字体是一样大的 点赞 评论 收藏
分享