Cover image for leetcode每日一题 P1513 仅含1的子串个数

leetcode每日一题 P1513 仅含1的子串个数

字数 141
阅读
访客

时间轴

时间轴

2025-11-16

init

字符串

题目:

只需要一次遍历。

1234567891011121314151617181920212223242526272829
#include <string>using std::string;class Solution {    public:	int numSub(string s)	{		long res = 0, left = 0, right = 0;		long len;		int n = s.size();		while (left <= right && right < n) {			// left找到第一个1			while (left < n && s[left] != '1') {				left++;			}			right = left;			while (right < n && s[right] == '1') {				right++;			}			// [left, right)			len = right - left;			res += (len + 1) * len / 2 % 1000000007;			left = right;		}		return res;	}};
评论加载中…