LeetCode题解(0696):二进制字符串中0和1数量相同的连续子串数量(Python)

    技术2022-07-10  134

    题目:原题链接(简单)

    标签:字符串

    解法时间复杂度空间复杂度执行用时Ans 1 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1)252ms (42.00%)Ans 2 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1)104ms (100.00%)Ans 3 (Python)

    解法一:

    def countBinarySubstrings(self, s: str) -> int: num0 = 0 num1 = 0 ans = 0 for i in range(len(s)): n = s[i] if i > 0 and s[i] != s[i - 1]: if s[i] == "0": num0 = 0 else: num1 = 0 if n == "1": if num0 > 0: ans += 1 num0 -= 1 num1 += 1 elif n == "0": if num1 > 0: ans += 1 num1 -= 1 num0 += 1 return ans

    解法二(减少判断次数):

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9JLUsvcC-1597651000056)(LeetCode题解(0696)]:截图1.png)

    def countBinarySubstrings(self, s: str) -> int: pre = 0 count = 1 curr = s[0] ans = 0 for n in s[1:]: if n == curr: count += 1 else: pre = count count = 1 curr = n if pre >= count: ans += 1 return ans
    Processed: 0.013, SQL: 9