题目来源:
leetcode题目,网址:2609. 最长平衡子字符串 - 力扣(LeetCode)
解题思路:
按要求进行模拟即可。
解题代码:
class Solution {
public int findTheLongestBalancedSubstring(String s) {
int res=0;
int zero=0;
int one=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='0'){
if(one==0){
zero++;
}else{
zero=1;
one=0;
}
}else{
one++;
if(one<=zero){
res=Math.max(res,2*one);
}
}
}
return res;
}
}
总结:
注意,当 1 的数量小于等于 0 的数量,可以组成一个长度为 1 的数量两倍的平衡字符串。文章来源:https://www.toymoban.com/news/detail-654869.html
无官方题解。文章来源地址https://www.toymoban.com/news/detail-654869.html
到了这里,关于题目:2609.最长平衡子字符串的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!