We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths, an array where widths[0] is the width of 'a', widths[1] is the width of 'b', ..., and widths[25] is the width of 'z'.

Now answer two questions: how many lines have at least one character from S, and what is the width used by the last such line? Return your answer as an integer list of length 2.

Example :
Input:
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"
Output: [3, 60]
Explanation:
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.
Example :
Input:
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"
Output: [2, 4]
Explanation:
All letters except 'a' have the same length of 10, and
"bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units.
For the last 'a', it is written on the second line because
there is only 2 units left in the first line.
So the answer is 2 lines, plus 4 units in the second line.

Note:

  • The length of S will be in the range [1, 1000].
  • S will only contain lowercase letters.
  • widths is an array of length 26.
  • widths[i] will be in the range of [2, 10].

这道题给了我们一个字符串,让我们把里面的字母写下来,规定了每一行的长度为100,然后每个字母的长度可以在widths数组中查询,说是如果某一个字母加上后超过了长度100的限制,那么就移动到下一行,问我们最终需要多少行,和最后一行的长度。这道题并没有太大的难度和技巧,就是楞头写呗,遍历所有的字母,然后查表得到其宽度,然后看加上这个新宽度是否超了100,超了的话,行数计数器自增1,并且当前长度为这个字母的长度,因为另起了一行。如果没超100,那么行长度就直接加上这个字母的长度。遍历完成后返回行数和当前行长度即可,参见代码如下:

class Solution {
public:
vector<int> numberOfLines(vector<int>& widths, string S) {
int cnt = , cur = ;
for (char c : S) {
int t = widths[c - 'a'];
if (cur + t > ) ++cnt;
cur = (cur + t > ) ? t : cur + t;
}
return {cnt, cur};
}
};

参考资料:

https://leetcode.com/problems/number-of-lines-to-write-string/solution/

https://leetcode.com/problems/number-of-lines-to-write-string/discuss/120666/Easy-Solution-6-lines-C++JavaPython

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Number of Lines To Write String 写字符串需要的行数的更多相关文章

  1. Leetcode806.Number of Lines To Write String写字符串需要的行数

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

  2. Java实现 LeetCode 806 写字符串需要的行数 (暴力模拟)

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

  3. [Swift]LeetCode806. 写字符串需要的行数 | Number of Lines To Write String

    We are to write the letters of a given string S, from left to right into lines. Each line has maximu ...

  4. Q806 写字符串需要的行数

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

  5. 806. Number of Lines To Write String - LeetCode

    Question 806. Number of Lines To Write String Solution 思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行. Java实现: p ...

  6. 806. Number of Lines To Write String

    806. Number of Lines To Write String 整体思路: 先得到一个res = {a : 80 , b : 10, c : 20.....的key-value对象}(目的是 ...

  7. 【Leetcode_easy】806. Number of Lines To Write String

    problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...

  8. 【LeetCode】806. Number of Lines To Write String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...

  9. LeetCode算法题-Number of Lines To Write String(Java实现)

    这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...

随机推荐

  1. Angular记录(1)

    文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...

  2. A fine property of the convective terms of axisymmetric MHD system, and a regularity criterion in terms of $\om^\tt$

    In [Zhang, Zujin; Yao, Zheng-an. 3D axisymmetric MHD system with regularity in the swirl component o ...

  3. Groovy 设计模式 -- 装饰器模式

    http://groovy-lang.org/design-patterns.html#_chain_of_responsibility_pattern 装饰器模式, 起到美化原始对象的作用. 一个被 ...

  4. python3中的socket

    socket是什么?用它做什么? socket,我们通俗的称之为套接字, 是进程间通信的一种方式,但是他与其他进程通信的一个主要区别是 他能实现不同主机间的通信,比如我们现在用的浏览器,在比如我们使用 ...

  5. windows7 java环境配置

    最近在工作碰到了在ie浏览器调用java程序时,出现了一些问题,所以在这里整理一下: jdk的下载地址为:http://666dx.pc6.com/wwb3/jdkx6417.zip 正对于windo ...

  6. js将时间戳转换为日期类型

    function getLocalTime(nS) {       var date = new Date(nS);    Y = date.getFullYear() + '年';    M = ( ...

  7. Nginx+Apache环境的安装与配置

    我们依然尽可能采用yum来安装我们需要的软件,由系统官方维护的软件,其安全性和稳定性都值得信赖,并且容易管理,升级方便,但是在CentOS和RHEL的官方yum源中暂时没有Nginx等软件包,所以我们 ...

  8. centos + nginx + php-fpm +mysql的简单配置

    安装前准备 安装约定(这个根据自己习惯,可自行修改) 1.软件源码包我都下载到/usr/local/src这个目录下 2.软件一般都编译安装到/usr/local这个目录下 安装基础库 yum -y ...

  9. python第六篇文件处理类型

    阅读目录 一 文件操作 二 打开文件的模式 三 操作文件的方法 四 文件内光标移动 五 文件的修改   文件处理                                             ...

  10. Spring 框架

    一. Spring入门 Spring模块都打包成JAR文件,其命名格式如下: spring-maluleName-x.y.z.RELEASE.jar 其中module name是模块的名字,而x.y. ...