class Solution {
public:
int findTheLongestBalancedSubstring(string s) {
int zeros = 0;
int ones = 0;
int ans = 0;
int n = s.size();
for(int i = 0; i < n; i++) {
int t = s[i] - '0';
if(t == 0) {
zeros++;
}
if(t == 1 && zeros) {
if(ones <= zeros) {
ones++;
}
}
if(i < n && (s[i + 1] - '0') == 0 && (s[i] - '0') == 1) {
int tmp = ones > zeros ? zeros : ones;
ans = ans > tmp ? ans : tmp;
zeros = 0;
ones = 0;
}
if(i == n - 1 && (s[i] - '0') == 1) {
int tmp = ones > zeros ? zeros : ones;
ans = ans > tmp ? ans : tmp;
zeros = 0;
ones = 0;
}
}
ans *= 2;
return ans;
}
};
class Solution {
public:
int findTheLongestBalancedSubstring(string s) {
int ans = 0;
int pre = 0;
int cur = 0;
int n = s.size();
for(int i = 0; i < n; i++) {
cur++;
if(i + 1 == n || s[i] != s[i + 1]) {
if(s[i] == '1') {
ans = max(ans, min(pre, cur) * 2);
}
pre = cur;
cur = 0;
}
}
return ans;
}
};
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!