题目描述:

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].

要完成的函数:

vector<int> numberOfLines(vector<int>& widths, string S)

说明:

1、这道题给定一个由字母组成的字符串和一个vector,vector长度为26,写着26个英文字母的宽度。要求把string中的字母写出来,按照vector中的长度写出来。

每一行最多只能有100个宽度,如果最后装不下了,那么就要写到第二行。

比如已经有了98个宽度了,现在要再写一个'a',‘a’的宽度为4,那么这时就必须写到第二行,第一行剩下两个宽度,第二行拥有四个宽度。

要求写出string中的所有字母,返回一共有多少行,和最后一行的宽度。

2、题意清晰,这道题也就变得异常容易了。

代码如下:

    vector<int> numberOfLines(vector<int>& widths, string S)
{
int s1=S.size(),sum=0,row=0;//sum记录每一行的宽度,row记录行数
for(int i=0;i<s1;i++)
{
sum+=widths[S[i]-'a'];
if(sum>100)
{
sum=widths[S[i]-'a'];//初始化下一行的宽度
row++;
}
}
return {row+1,sum};
}

实测2ms,beats 100% of cpp submissions。

leetcode-806-Number of Lines To Write String的更多相关文章

  1. LeetCode 806 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 m ...

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

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

  3. 806. Number of Lines To Write String

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

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

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

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

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

  6. [LeetCode&Python] Problem 806. 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 ...

  7. 806. Number of Lines To Write String (5月24日)

    解答 class Solution { public: vector<int> numberOfLines(vector<int>& widths, string S) ...

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

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

  9. [LeetCode] 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 ...

  10. leetCode题解之Number of Lines To Write String

    1.题目描述 2.分析 使用一个map将字母和数字对应起来,方便后续使用. 3.代码 vector<int> numberOfLines(vector<int>& wi ...

随机推荐

  1. Python简单邮件发送源码

    环境: Python27 主要代码: # -*- coding: utf-8 -*- ''' Created on 2016年10月18日 @author: xuxianglin ''' import ...

  2. DOM0级与DOM2级

    定义: 0级DOM 分为2个:一是在标签内写onclick事件  二是在JS写onlicke=function(){}函数 1) <input id="myButton" t ...

  3. Springboot-读取核心配置文件及自定义配置文件

    读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. 核心 ...

  4. 面向对象的JavaScript-004

    1. // Below is an example of how to use Object.create() to achieve classical inheritance. This is fo ...

  5. WindowServer2016无法安装.netframework3.5

    因为安装sql server的原因 需要安装.NET Framework3.5 报错内容如下: 原因分析 找不到安装源文件. 解决办法 可以通过如下 PowerShell 脚本进行安装: 从开始菜单中 ...

  6. HDU 4514 湫湫系列故事――设计风景线 (树形DP)

    题意:略. 析:首先先判环,如果有环直接输出,用并查集就好,如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存, 还有用C++交用的少一些,我用G++交的卡在32764 ...

  7. CodeForces 289B Polo the Penguin and Matrix (数学,中位数)

    题意:给定 n * m 个数,然后每次只能把其中一个数减少d, 问你能不能最后所有的数相等. 析:很简单么,首先这个矩阵没什么用,用一维的存,然后找那个中位数即可,如果所有的数减去中位数,都能整除d, ...

  8. 一些..C#知识点总结

    C# 知识点汇总 (其实C#与Java多少有区别,对于咱这个幼儿园大班生来说) 1.认识C#程序 (1)namespqce关键字 namespqce(命名空间)是C#组织代码的方式,它的作用类似于Ja ...

  9. Thrift辅助类,用于简化Thrift编程

    CThriftServerHelper用于服务端,CThriftClientHelper用于客户端. IDL定义: service PackageManagerService { } 服务端使用示例: ...

  10. Reporting Service服务SharePoint集成模式安装配置(5、安装 SQL SERVER 2012 SP1产品)

    有过SQL2012 数据库安装经验的,可以跳过这一步骤直接进入第五步骤:RS外接程序的安装  数据库安装工具:SQLServer2012 SP1 Name:SQLServer2012SP1-FullS ...