??題目來(lái)源:
? ? ? ? leetcode題目,網(wǎng)址:2609. 最長(zhǎng)平衡子字符串 - 力扣(LeetCode)
解題思路:
? ? ? ?按要求進(jìn)行模擬即可。
解題代碼:
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;
}
}
總結(jié):
? ? ? ? 注意,當(dāng) 1 的數(shù)量小于等于 0 的數(shù)量,可以組成一個(gè)長(zhǎng)度為 1 的數(shù)量?jī)杀兜钠胶庾址?mark hidden color="red">文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-654869.html
? ? ? ? 無(wú)官方題解。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-654869.html
到了這里,關(guān)于題目:2609.最長(zhǎng)平衡子字符串的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!