LeetCode 力扣官方题解 | 806. 写字符串需要的行数

LeetCode 力扣官方题解 | 806. 写字符串需要的行数

  1. 写字符串需要的行数
    「亚马逊、谷歌Google、奥多比Adobe」考题

题目描述

难易度:简单

我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为 100 个单位,如果我们在写某个字母的时候会使这行超过了 100 个单位,那么我们应该把这个字母写到下一行。

我们给定了一个数组 widths ,这个数组 widths [0] 代表 ‘a’ 需要的单位,widths [1] 代表 ‘b’ 需要的单位,…, widths [25] 代表 ‘z’ 需要的单位。

现在回答两个问题:至少多少行能放下 S ,以及最后一行使用的宽度是多少个单位?将你的答案作为长度为 2 的整数列表返回。

示例 1:

输入: 
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
输出: [3, 60]
解释: 
所有的字符拥有相同的占用单位10。所以书写所有的26个字母,
我们需要2个整行和占用60个单位的一行。

示例 2:

输入: 
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "bbbcccdddaaa"
输出: [2, 4]
解释: 
除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位.
最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。
所以,这个答案是2行,第二行有4个单位宽度。

注:
字符串 S 的长度在 [1, 1000] 的范围。

S 只包含小写字母。

widths 是长度为 26 的数组。

widths [i] 值的范围在 [2, 10] 。

解决方案

方法一:直接遍历

思路与算法

我们从左到右遍历字符串 s 中的每个字母,lines 表示当前书写所需的行数,width 表示当前行已经使用的宽度。当遍历到一个字母 c 时:

如果 width + widths [c] ≤ 100 ,此时那么更新 width = width + widths [c] 并保持 lines 不变;

如果 width + widths [c] > 100 ,此时需要另起新的一行,那么此时 lines 的值加 1 ,并将 width 置为 widths [c] 。

代码

Python3

class Solution:
    def numberOfLines(self, widths: List[int], s: str) -> List[int]:
        MAX_WIDTH = 100
        lines, width = 1, 0
        for c in s:
            need = widths[ord(c) - ord('a')]
            width += need
            if width > MAX_WIDTH:
                lines += 1
                width = need
        return [lines, width]

Java

class Solution {
    public static final int MAX_WIDTH = 100;

    public int[] numberOfLines(int[] widths, String s) {
        int lines = 1;
        int width = 0;
        for (int i = 0; i < s.length(); i++) {
            int need = widths[s.charAt(i) - 'a'];
            width += need;
            if (width > MAX_WIDTH) {
                lines++;
                width = need;
            }
        }
        return new int[]{lines, width};
    }
}

C++

const int MAX_WIDTH = 100;

class Solution {
public:
    vector<int> numberOfLines(vector<int>& widths, string s) {
        int lines = 1;
        int width = 0;
        for (auto & c : s) {
            int need = widths[c - 'a'];
            width += need;
            if (width > MAX_WIDTH) {
                lines++;
                width = need;
            }
        }
        return {lines, width};
    }
};

C#

public class Solution {
    public static int MAX_WIDTH = 100;

    public int[] NumberOfLines(int[] widths, string s) {
        int lines = 1;
        int width = 0;
        for (int i = 0; i < s.Length; i++) {
            int need = widths[s[i] - 'a'];
            width += need;
            if (width > MAX_WIDTH) {
                lines++;
                width = need;
            }
        }
        return new int[]{lines, width};
    }
}

C

#define MAX_WIDTH 100

int* numberOfLines(int* widths, int widthsSize, char * s, int* returnSize){
    int lines = 1;
    int width = 0;
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
        int need = widths[s[i] - 'a'];
        width += need;
        if (width > MAX_WIDTH) {
            lines++;
            width = need;
        }
    }
    int * ans = (int *)malloc(sizeof(int) * 2);
    *returnSize = 2;
    ans[0] = lines;
    ans[1] = width;
    return ans;
}

Golang

func numberOfLines(widths []int, s string) []int {
    const maxWidth = 100
    lines, width := 1, 0
    for _, c := range s {
        need := widths[c-'a']
        width += need
        if width > maxWidth {
            lines++
            width = need
        }
    }
    return []int{lines, width}
}

JavaScript

const MAX_WIDTH = 100;
var numberOfLines = function(widths, s) {
    let lines = 1;
    let width = 0;
    for (let i = 0; i < s.length; i++) {
        const need = widths[s[i].charCodeAt() - 'a'.charCodeAt()];
        width += need;
        if (width > MAX_WIDTH) {
            lines++;
            width = need;
        }
    }
    return [lines, width];
};

复杂度分析

时间复杂度:O(n) ,其中 n 为字符串 s 的长度。需要遍历一遍字符串 s ,求出行数。
空间复杂度:O(1) 。

获取更多详情:LeetCode 力扣官方题解 | 806. 写字符串需要的行数

编辑&版式:pingping

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值