806. Number of Lines To Write String - LeetCode

Question

806. Number of Lines To Write String

Solution

思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行。

Java实现:

public int[] numberOfLines(int[] widths, String S) {
    int left = 0;
    int lines = 0;
    for (char c : S.toCharArray()) {
        left += widths[c - 'a'];
        if (left >= 100) {
            lines ++;
            left = left > 100 ? widths[c - 'a'] : 0;
        }
    }
    lines += left > 0 ? 1 : 0;
    return new int[]{lines, left};
}

猜你喜欢

转载自www.cnblogs.com/okokabcd/p/9198213.html
今日推荐