题目描述
解法:
简单模拟一下,lines
表示写了多少行,cursor
表示当前填充光标所在位置
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string s) {
int lines = 0, cursor = 0;
for (auto c: s)
{
int t = widths[c - 'a'];
if (cursor + t > 100)
{
lines++;
cursor = t;
}
else cursor += t;
}
if (cursor != 0) lines++;
return {lines, cursor};
}
};