前一天忘了记录了,补一下
1.题目
2.求解
这也是一道很简单的题目,但是我wa了一次,属于是太粗心了。常用的用char - 'a'得到index去数组里查找对应的width即可。
一定需要注意的是他需要判断每一行的末尾的那个字母是否会超出行距,知道这一点,其他的就都很简单了。
3.代码
class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string s) {
vector<int> ans;
int cnt = 0;
int height = 1;
for(int i = 0; i < s.size(); i++){
int wid = widths[int(s[i] - 'a')];
if(cnt + wid > 100) {
cnt = 0;
height ++;
}
cnt += wid;
}
ans.push_back(height);
ans.push_back(cnt);
return ans;
}
};