0
点赞
收藏
分享

微信扫一扫

LeetCode 806. 写字符串需要的行数

小禹说财 2022-04-14 阅读 63

解题思路:

简单的直接遍历,阈值溢出并计数。

代码:

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为字符串长度。

举报

相关推荐

0 条评论