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. Spring Cloud使用样例

    Spring Cloud Demo 项目地址:https://github.com/hackyoMa/spring-cloud-demo 组件 基于Spring Boot 2.0.4.Spring C ...

  2. 使用docker部署SqlServer

    踩了很多坑,来记录一下 首先说sqlserver 1. 安装docker要使用centos 7以上版本,使用centos 6及以下版本会出现各种问题 2. docker CE安装过程 $ sudo y ...

  3. 携程apollo系列-客户端集成

    本文讲解如何在 Java 程序中集成 Apollo 配置, 主要涉及到一些基础用法. 对于一些高级用法, 比如如何加密/解密配置项 (可用于数据库密码配置), 如何动态切换数据源地址,如何动态切换日志 ...

  4. input全选和取消全选

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  5. Django的Admin站点管理

    一.概述 内容发布:负责添加.修改.删除内容 公告访问 二.配置Admin应用 在settings.py文件中的INSTALLED_APPS中添加'django.contrib.admin',默认是添 ...

  6. 个人经验~ 利用5.7的sys库更好的排查问题

    一 简介:今天我们讲讲如何利用5.7的sys新库进行问题的排查二 描述   1 Sys库所有的数据源来自:performance_schema和information_schema.目标是把perfo ...

  7. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

  8. 【原创】大叔问题定位分享(6)Dubbo monitor服务iowait高,负载高

    一 问题 Dubbo monitor所在服务器状态异常,iowait一直很高,load也一直很高,监控如下: iowait如图: load如图: 二 分析 通过iotop命令可以查看当前系统中磁盘io ...

  9. 什么是内部类? Static Nested Class 和 Inner Class 的不同。

    什么是内部类? Static Nested Class 和 Inner Class 的不同.        内部类就是在一个类的内部定义的类,内部类中不能定义静态成员(静态成员不是对象的特性,只是为了 ...

  10. git remote: HTTP Basic: Access denied Mac 使用Sourcetree 密码输错 再次输入解决方案

    删除下面的key即可