题目描述:

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. iOS端一次视频全屏需求的实现(转)

    对于一个带有视频播放功能的app产品来说,视频全屏是一个基本且重要的需求.虽然这个需求看起来很简单,但是在实现上,我们前后迭代了三套技术方案.这篇文章将介绍这三种实现方案中的利弊和坑点,以及实现过程中 ...

  2. DNS处理模块dnspython

    一.介绍 官网:http://www.dnspython.org/ https://pypi.org/project/dnspython/ dnspython是Python的DNS工具包.它支持几乎所 ...

  3. DSA 算法

    一.简介 DSA算法是Schnorr和ElGamal签名算法的变种,被美国NIST作为DSS(DigitalSignature Standard).它是一种公开密钥算法,用作数字签名. http:// ...

  4. It's not too late to start!

    It's not too late to start! 以此鼓励,希望能坚持下去,一个半路自学PHP的准PHPer!

  5. spark reduceByKey

    reduce(binary_function) reduce将RDD中元素前两个传给输入函数,产生一个新的return值,新产生的return值与RDD中下一个元素(第三个元素)组成两个元素,再被传给 ...

  6. OSG相机与视图

    转自:http://blog.csdn.net/wang15061955806/article/details/51603083 相机与视图     osg::Camera类用来管理OSG中的模型—— ...

  7. css3系列之animation

    在上次博文中已经讲了transition,其实animation与transition功能相同,都是通过改变元素 的属性来实现动画效果的.但是它们也有区别:transition是只能通过改变指定属性的 ...

  8. iPhone Development – core data relationships tutorial part 1

    I’m going to start a short series on Core Data relationships and maybe throw in some general Core Da ...

  9. (二)ASP.NET中JavaScript的中英文(多语言)实现方案(二)

    在ASP.NET中JavaScript的中英文(多语言)实现方案中简单的介绍了js实现多语言的一种方案.下面将要讲述另外一种方法,尽管很相似,但是有些地方也是需要细细琢磨的,不说了,先看看. 在Lan ...

  10. Android-广播概念

    Android中的消息机制 1.Handler+Message消息机制,是用于子线程与主线程的通讯: 2.广播+广播接收者也是消息机制,是重量级别的,四大组件之一,需要激活组件,是用于组件和组件之间通 ...