题目要求

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. <王川自选集第一卷电子书 >读书笔记

    什么叫限定自己能力的边界?就是接受承认一个事实,世界上大多数东西你并不懂,或者一知半解,懂得很肤浅.只有少数东西,由于你可以天天深入的接触,你才有深刻的理解. 购买一个公司的股票,却从未使用其核心产品 ...

  2. UML类建模(强烈推荐-思路很清晰)

    UML类建模(强烈推荐-思路很清晰) 2016年10月23日 15:17:47 mbshqqb 阅读数:2315 标签: uml面向对象设计模式 更多 个人分类: 面向对象程序设计   UML的构造快 ...

  3. plsql常用方法-转

    在SQLPLUS下,实现中-英字符集转换alter session set nls_language='AMERICAN';alter session set nls_language='SIMPLI ...

  4. MyCAT简易入门 (Linux)

    MyCAT是mysql中间件,前身是阿里大名鼎鼎的Cobar,Cobar在开源了一段时间后,不了了之.于是MyCAT扛起了这面大旗,在大数据时代,其重要性愈发彰显.这篇文章主要是MyCAT的入门部署. ...

  5. python风格的抽象工厂模式

    抽象工厂模式: 提供一个接口,用户创建多个相关或依赖对象,而不需要指定具体类. 原则: 依赖抽象,不依赖具体类. 实例: 用不同原材料制作不同口味的披萨,创建不同原材料的工厂,不同实体店做出口味不同的 ...

  6. c#扩展函数

    分页 public static class IEnumerableExt { public static (IEnumerable<T> dataAfterPaging, Pageinf ...

  7. 面试Spring之bean的生命周期

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  8. Linux-C实现GPRS模块发送短信

    “GSM模块,是将GSM射频芯片.基带处理芯片.存储器.功放器件等集成在一块线路板上,具有独立的操作系统.GSM射频处理.基带处理并提供标准接口的功能模块.GSM模块根据其提供的数据传输速率又可以分为 ...

  9. jenkins实战(二):构建自由风格的maven项目

    本系列打算全面介绍jenkins的常规使用,这是第二篇,之前的文章在: jenkins实战(一):war安装及插件安装 一.新建项目 1.新建项目 此处我们打算新建自由风格项目,见下图. 值得注意的是 ...

  10. JS正则校验

    /** 用途:检查输入字符串是否为空或者全部都是空格 输入:str:字符串 返回: 如果全是空返回true,否则返回false */ function isNull(str) { if (str == ...