题目链接
思路分析
遍历字符串,找到当前字串最大的长度,其对应的总数使从1累加到n。
代码
class Solution {
public:
const int mod = 1e9 + 7;
int numSub(string s) {
int l = 0, r = 0,ans = 0,n = s.size();
s += '0';
while(r < n){
while(s[l] == '0') l++;
r = l;
while(s[r] == '1') r++;
int tot = r - l;
for(int i = 1;i <= tot;i++)
ans = (ans + i) % mod;
l = r;
}
return ans;
}
};