免费网络咨询免费建站,泰安公司网站开发,中国十大人力资源公司,wordpress 同步微信素材2023-11-08每日一题
一、题目编号
2609. 最长平衡子字符串二、题目链接
点击跳转到题目位置
三、题目描述
给你一个仅由 0 和 1 组成的二进制字符串 s 。
如果子字符串中 所有的 0 都在 1 之前 且其中 0 的数量等于 1 的数量#xff0c;则认为 s 的这个子字符串是平衡子…2023-11-08每日一题
一、题目编号
2609. 最长平衡子字符串二、题目链接
点击跳转到题目位置
三、题目描述
给你一个仅由 0 和 1 组成的二进制字符串 s 。
如果子字符串中 所有的 0 都在 1 之前 且其中 0 的数量等于 1 的数量则认为 s 的这个子字符串是平衡子字符串。请注意空子字符串也视作平衡子字符串。
返回 s 中最长的平衡子字符串长度。
子字符串是字符串中的一个连续字符序列。
提示
1 s.length 50‘0’ s[i] ‘1’
四、解题代码
class Solution {
public:int findTheLongestBalancedSubstring(string s) {int res 0, n s.size();vectorint count(2);for (int i 0; i n; i) {if (s[i] 1) {count[1];res max(res, 2 * min(count[0], count[1]));} else if (i 0 || s[i - 1] 1) {count[0] 1;count[1] 0;} else {count[0];}}return res; }
};
五、解题思路
(1) 计数