解题思路:
简单的直接遍历,阈值溢出并计数。
代码:
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string s) {
vector<int> ans = {1, 0};
for(auto ch : s)
{
if(ans[1] + widths[ch - 'a'] > 100) {
ans[1] = widths[ch - 'a'];
ans[0]++;
}
else ans[1] += widths[ch - 'a'];
}
return ans;
}
};
时间复杂度O(n),空间复杂度O(1)。n为字符串长度。