Leetcode 806: 写字符串需要的行数

220412 每日一题,非常简单


class Solution {
public:
    vector<int> numberOfLines(vector<int>& widths, string s) {
        int rest=0,linenum=1;
        for(int i=0;i<s.length();i++){
            if(rest == 100 && widths[s[i]-'a']>0){
                rest = widths[s[i]-'a'];
                linenum++;
                continue;
            }
            
            rest += widths[s[i]-'a'];
            if(rest>100){
                rest = widths[s[i]-'a'];
                linenum++;
            }
        }
        return vector<int>{linenum,rest};
    }
};
0%