1513. 仅含 1 的子串数

Medium

思路

看数据规模,可以判断代码的时间复杂度控制在O(n)

以上,尝试写一下代码,AC!

代码

python3

class Solution:
    def numSub(self, s: str) -> int:
      i = 0
      j = 0
      res = 0
      for i in range(len(s) + 1):
        if i == len(s) or s[i] == '0':
          res += ((i-j) * (i-j+1) // 2) % (10**9+7)
          j = i + 1
      return res