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

This problem is very simple.

class Solution:
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
NumOfLine=1
currentWidth=0
iA=ord('a') for c in S:
widthNo=ord(c)-iA
cwidth=widths[widthNo]
if currentWidth+cwidth>100:
NumOfLine+=1
currentWidth=cwidth
else:
currentWidth+=cwidth return NumOfLine,currentWidth

  

[LeetCode&Python] Problem 806. Number of Lines To Write String的更多相关文章

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

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

  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】806. Number of Lines To Write String 解题报告(Python)

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

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

  6. [LeetCode&Python] Problem 447. Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  7. [LeetCode&Python] Problem 476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  8. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

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

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

随机推荐

  1. [ios]"The identity used to sign the executable is no longer valid"错误解决方法

    重新去开发者网站,申请一遍profiles 参考:http://www.bubuko.com/infodetail-982908.html 我出现这个错误的情况,程序提交app store之后,第二天 ...

  2. Java8 新特性之默认接口方法

    摘要: 从java8开始,接口不只是一个只能声明方法的地方,我们还可以在声明方法时,给方法一个默认的实现,我们称之为默认接口方法,这样所有实现该接口的子类都可以持有该方法的默认实现. · 待定 一. ...

  3. URAL 2072 Kirill the Gardener 3

    URAL 2072 思路: dp+离散化 由于湿度的范围很大,所以将湿度离散化 可以证明,先到一种湿度的最左端或者最右端,然后结束于最右端或最左端最优,因为如果结束于中间,肯定有重复走的路 状态:dp ...

  4. Android Studio apk打包,keystore.jks文件生成,根据keystore密钥获取SHA1安全码

    keystore.jks文件生成,打包APK 选择Build > Generate Signed APK 出现如下弹框: 然后点击Create new...(创建的意思)出现另一个弹框,在做如下 ...

  5. Android之省市区三级联动

    最近项目要做一个电商APP,选择收货地址的三级联动滚动选择组件, 控件用起来非常简单 ,下面是它的运行效果: 布局 <LinearLayout xmlns:android="http: ...

  6. 『PyTorch』第五弹_深入理解Tensor对象_上:初始化以及尺寸调整

    一.创建Tensor 特殊方法: t.arange(1,6,2)t.linspace(1,10,3)t.randn(2,3) # 标准分布,*size t.randperm(5) # 随机排序,从0到 ...

  7. zzuli 1875多线DP

    1875: 蛤玮的财宝 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 530  Solved: 116 SubmitStatusWeb Board De ...

  8. Leetcode 78

    //和77类似的问题,可以放在一起记忆class Solution { public: vector<vector<int>> subsets(vector<int> ...

  9. LOV抛出值无效的异常

    在选择LOV中的行返回之后,点击提交的时候始终报值无效的异常. 症状:LOV中有值为   XXX项目,XXX项目(一期) 的时候,当你选择XXX项目 再进行提交,老是报出值无效的异常. 解决方式:在页 ...

  10. Activiti的后台数据库表详解

    Activiti的后台是有数据库的支持,所有的表都以ACT_开头. 第二部分是表示表的用途的两个字母标识.用途也和服务的API对应. 1)       ACT_RE_*: 'RE'表示reposito ...