给你一个二进制字符串 s(仅由 '0' 和 '1' 组成的字符串)。
返回所有字符都为 1 的子字符串的数目。
由于答案可能很大,请你将它对 10^9 + 7 取模后返回。
示例 1:
输入:s = "0110111"
输出:9
解释:共有 9 个子字符串仅由 '1' 组成
"1" -> 5 次
"11" -> 3 次
"111" -> 1 次
示例 2:
输入:s = "101"
输出:2
解释:子字符串 "1" 在 s 中共出现 2 次
示例 3:
输入:s = "111111"
输出:21
解释:每个子字符串都仅由 '1' 组成
示例 4:
输入:s = "000"
输出:0
提示:
s[i] == '0' 或 s[i] == '1'
1 <= s.length <= 10^5
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-substrings-with-only-1s
class Solution:
def numSub(self, s: str) -> int:
list_1 = s.split("0")
i = 0
results = 0
while i < len(list_1):
if list_1[i]:
# results = results + sum(list(range(len(list_1[i]) + 1)))
results = results + sum(range(len(list_1[i]) + 1))
i += 1
# results = results % (10^9 + 7) # 数值溢出报错, 原因????
# 题目中:10^9 是 math.pow(10, 9), 而不是 10^9(异或) 卡在这 傻逼了
import math
results = int(results % (math.pow(10, 9) + 7))
return results
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)