题目描述:

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. Linux ftp Command

    一.ftp的get命令和mget命令有何不同? get一次只下载一个文件:mget一次可以下载多个文件,而且支持通配符,需要注意的是在mget的时侯,需要对每一个文件都选择y/n,如果想不交互的下载全 ...

  2. 移动端flexbox的小tips

    我也是看了腾讯isux的博客,解答了我关于flexbox一个很长时间的疑惑,就是flex布局在安卓手机会出现内容长短不同导致不均分的现象. 具体的内容可以去看腾讯isux的博客,地址在这:https: ...

  3. [SoapUI] JsonPath is to JSON what XPath is to XML

    1.通过JsonUtil验证Json的有效性 2.两种方式通过JPath读取Json的内容

  4. zigbee之IAR环境搭建

    注册机第一个要选择: 为什么? 之前说CC2530是支持zigbee协议的无线芯片,其实它是这款硬件上有一个支持zigbee协议的无线电路,不仅有这款电路,而且还有一块cpu电路,它就是8051cpu ...

  5. SpringMVC 细节学习

    使用Spring MVC,配置DispatcherServlet是第一步  DispatcherServlet是前置控制器,配置在web.xml文件中的 .拦截匹配的请求,Servlet拦截匹配规则要 ...

  6. 下拉菜单--JavaScript触发方法

    1. $(function(){ $(".dropdown-toggle").one("click",function(){ $(this).dropdown( ...

  7. (2)WePHP 控制器与使用模板

    <?php class C_index extends Action { public function __initialize() { echo"自动执行"; } pub ...

  8. C# 判断是否是在设计模式下有效的方法

    public static bool IsDesignMode() { bool returnFlag = false; #if DEBUG if (LicenseManager.UsageMode ...

  9. 记一次阿里云linux病毒清理过程

    1.起因   因为这台服务器是我们公司内部开发服务器,几乎每个人都有root密码.在两天前突然有同事反馈说偶尔会有ssh连不上,git代码无法提交的问题,刚开始也没有在意,以为是阿里云服务器网络波动的 ...

  10. redis整理の走进redis世界

    声明:原文摘自http://weibo.com/u/2446082491,谢谢他的分享! 在当前大型互联网应用以及提供云计算服务的时候,怎样保证系统在海量数据环境下的高性 能.高可靠性.高扩展性.高可 ...