计数二进制子串
# 📃 题目描述
题目链接:696. 计数二进制子串 (opens new window)
# 🔔 解题思路
class Solution {
public int countBinarySubstrings(String s) {
// 记录每个连续数字串的数量
List<Integer> counts = new ArrayList<>();
int left = 0, right = 0;
while (right < s.length()) {
if (s.charAt(right) == s.charAt(left)) {
right ++;
}
else {
counts.add(right - left);
left = right;
}
}
counts.add(right - left);
int res = 0;
for (int i = 0; i < counts.size() - 1; i ++) {
res += Math.min(counts.get(i), counts.get(i + 1));
}
return res;
}
}
# 💥 复杂度分析
- 空间复杂度:
- 时间复杂度:
🎁 公众号

各位小伙伴大家好呀,叫我小牛肉就行,目前在读东南大学硕士,上方扫码关注公众号「飞天小牛肉」,与你分享我的成长历程与技术感悟~
帮助小牛肉改善此页面 (opens new window)
Last Updated: 2023/02/16, 11:27:10