小禹说财

关注

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

小禹说财

关注

阅读 63

2022-04-14

解题思路:

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

代码:

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

相关推荐

冬冬_79d4

LeetCode 806.写字符串需要的行数(简单)

冬冬_79d4 59 0 0

前端王祖蓝

806. 写字符串需要的行数

前端王祖蓝 59 0 0

pipu

[Leetcode每日一题]806. 写字符串需要的行数

pipu 117 0 0

野见

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

野见 66 0 0

七千22

806. 写字符串需要的行数 : 简单模拟题

七千22 29 0 0

梅梅的时光

leetcode-写字符串需要的行数

梅梅的时光 69 0 0

zibianqu

【leetcode】 写字符串需要的行数 c++ python

zibianqu 64 0 0

DT_M

【Leetcode-每日一题】写字符串需要的行数

DT_M 106 0 0

唯米天空

LeetCode题解(0806):写字符串所需的行数(Python)

唯米天空 40 0 0

把平平凡凡的生活过成诗与远方

Leetcode-Easy 806. Number of Lines To Write String

把平平凡凡的生活过成诗与远方 67 0 0

精彩评论(0)

0 0 举报