关于s.length()
查看原帖
关于s.length()
519605
Seven_tc楼主2025/1/19 00:06

我好像记得string s; s.length();的时间复杂度是O(1)啊,怎么回事 我查了两个小时错误,

		for (int i = 0; i <= this->s.length()-3; i++) {
			if (this->s[i] == 's' && this->s[i + 1] == 'o' && this->s[i + 2] == 's')
				this->c++;
		}

这样写最后三个测试点会超时,而且测试时好像卡住了一样,但是

		this->len = s.length() - 3;
		for (int i = 0; i <= this->len; i++) {
			if (this->s[i] == 's' && this->s[i + 1] == 'o' && this->s[i + 2] == 's')
				this->c++;
		}

这样写就没有问题,有没有大佬告诉我为什么

2025/1/19 00:06
加载中...