携程第四题,不清楚是不是最简洁
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语言,比我司面试题还水。
相关推荐
06-18 22:40
吉林大学 Java 点赞 评论 收藏
分享
点赞 评论 收藏
分享