题目要求

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.

题目分析及思路

题目给出一个字符串和每个字母的长度数组,且限制每一行的最大值是100个单位,要求得到最后的行数和最后一行的宽度。可以遍历该字符串,每100个单位判断一下。

python代码

class Solution:

def numberOfLines(self, widths: 'List[int]', S: 'str') -> 'List[int]':

lines, width = 1, 0

for s in S:

w = widths[ord(s) - ord('a')]

width += w

if width > 100:

lines += 1

width = w

return [lines, width]

LeetCode 806 Number of Lines To Write String 解题报告的更多相关文章

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

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

  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】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  6. 【LeetCode】434. Number of Segments in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...

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

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

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

  9. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

随机推荐

  1. vs2015打开慢的解决方法

        1.首先是这里,这里默认是用的软件加速,把"基于客户端性能自动调整视觉体验"去掉勾选.然后把下面的第一个选项去掉,第二选项勾选.我在想,它的"自动"基于 ...

  2. java解析Excel(xls、xlsx两种格式)

    https://www.cnblogs.com/hhhshct/p/7255915.html ***************************************************** ...

  3. Ubuntu 卸载重装 IntelliJ Idea Community

    参考: https://stackoverflow.com/questions/22983101/how-to-uninstall-intellij-idea-on-ubuntu-13-10 @SLH ...

  4. 【WPF】点击滑动条(Slider),移动滑块(Tick)到鼠标点击的位置

    问题:点击Slider控件时,滑块会自动跳到滑动条的最边缘位置,无法跳到鼠标点击的位置上. 办法:给Slider控件设置属性IsMoveToPointEnabled="True"即 ...

  5. git-ftp代码部署方式

    虽然如今ci方法已经在很多团队使用了,但对于一些个人性的基于PHP的跑在虚拟主机的小项目,既没有服务端的Git环境,又不想时刻跑一个Genkins,就只能回到原始的FTP上传了. 所幸有了git-ft ...

  6. 第三百九十三节,Django+Xadmin打造上线标准的在线教育平台—Xadmin后台进阶开发配置

    第三百九十三节,Django+Xadmin打造上线标准的在线教育平台—Xadmin后台进阶开发配置 设置后台某个字段的排序规则 在当前APP里的adminx.py文件里的数据表管理器里设置 order ...

  7. centos6.5安装mongodb2.6

    下载地址:http://www.mongodb.org/downloads 解压命令:tar zxf mongodb-linux-i686-2.6.0.tgz 存放目录:/usr/local/mong ...

  8. 编译使用tensorflow c版本动态链接库

    注意:如果是linux或mac os操作系统,可以参考https://www.tensorflow.org/install/install_c,直接下载对应的so库和头文件,然后跳到步骤4.如果不能使 ...

  9. Android进阶——深入浅出Handler(一)

    Android进阶--深入浅出Handler(一) 在学习Handler之前,首先要学习一些基本概念,这将对之后的学习有所帮助. 主线程:Main Thread,又叫UI线程(UI Thread).A ...

  10. IOC容器特性注入第二篇:初始引擎查找对应的IOC容器

    上篇文章介绍了如何利用反射类查找网站bin文件夹下面所有DLL的程序集类,这篇文章将介绍如何初始化一个IOC容器引擎. 目前IOC容器有很多,如Ninject,Autofac等,每个容器的驱动都不一样 ...