老师这个方法有点笨吧
来源:2-5 计算子串代码演示

weixin_慕斯5231059
2019-11-02
通用解法:
var countBinarySubstrings = function (s) {
const len = s.length;
let n = 0,
pre = 0,
current = 1;
for (let i = 0; i < len; i++) {
if (s[i] === s[i + 1]) {
current++;
} else {
//当发现第三次不同时,前两组中最小的个数就是匹配出来的组数,比如00011,就有2组,一次类推
if (pre > 0) {
n += Math.min(pre, current);
}
pre = current;
current = 1;
}
}
return n;
};
写回答
1回答
-
快乐动起来呀
2019-11-02
这个方法看上去不是最优的,不过可以在LeetCode跑一下,算法这种东西一上来不可能找到最优解的,先锻炼自己的思路,然后不断去发现瓶颈在哪,然后再想办法优化,按这个思路下去就能得到更好的算法
00
相似问题