0
点赞
收藏
分享

微信扫一扫

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

DT_M 2022-04-14 阅读 106
leetcode

写字符串需要的行数
难度:简单
在这里插入图片描述
做题三分钟,理解题意半小时。。。
题目的意思就是按照S的顺序来输出字符,widths数组记录每个字符需要输出的次数,根据题意模拟过程即可。

代码如下:

	public int[] numberOfLines(int[] widths, String s) {
        int[] res = new int[2];
        char[] chars = s.toCharArray();
        int cnt = 0;
        int row = 1;
        for (char c : chars) {
            if (cnt + widths[c-'a']>100){
                cnt = widths[c-'a'];
                row++;
            }else{
                cnt += widths[c-'a'];
            }
        }
        res[0] = row;
        res[1] = cnt;
        return res;
    }

执行结果:成功
在这里插入图片描述

举报

相关推荐

0 条评论