806 LeetCode 写字符串需要的行数

题目描述:
在这里插入图片描述

思路:
遍历S,计算行值并且判断即可

代码如下:

class Solution {
public:
    vector<int> numberOfLines(vector<int>& widths, string S) {
        vector<int>res;
        int sum=0;
        int cnt=0;
        for(int i=0;i<S.size();i++){
            cnt+=widths[S[i]-97];
            if(cnt>100&&(cnt-widths[S[i]-97])<=100){
                sum++;
                cnt=widths[S[i]-97];
            }
        }
        res.push_back(sum+1);
        res.push_back(cnt);
        return res;
    }
};
发布了123 篇原创文章 · 获赞 0 · 访问量 947

猜你喜欢

转载自blog.csdn.net/peachzy/article/details/104390716