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 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 解题报告的更多相关文章
- 【LeetCode】806. Number of Lines To Write String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...
- 806. Number of Lines To Write String - LeetCode
Question 806. Number of Lines To Write String Solution 思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行. Java实现: p ...
- 806. Number of Lines To Write String
806. Number of Lines To Write String 整体思路: 先得到一个res = {a : 80 , b : 10, c : 20.....的key-value对象}(目的是 ...
- 【Leetcode_easy】806. Number of Lines To Write String
problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
- [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 ...
- 806. Number of Lines To Write String (5月24日)
解答 class Solution { public: vector<int> numberOfLines(vector<int>& widths, string S) ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
随机推荐
- MyBatis源码分析-基础支持层反射模块Reflector/ReflectorFactory
本文主要介绍MyBatis的反射模块是如何实现的. MyBatis 反射的核心类Reflector,下面我先说明它的构造函数和成员变量.具体方法下面详解. org.apache.ibatis.refl ...
- TWELP™ Vocoder
TWELP™ Vocoder DSP Innovations Inc. (DSPINI) announces new class of proprietary vocoders for wide ...
- 基于jQuery鼠标滚轮滑动到页面节点部分
基于jQuery鼠标滚轮滑动到页面节点部分.这是一款基于jQuery+CSS3实现的使用鼠标滚轮或者手势滑动到页面节点部分特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: &l ...
- 推荐几个Windows工具软件: ASuite - 便携的程序启动器
主页: http://asuite.sourceforge.net 下载: http://sourceforge.net/projects/asuite/ ASuite is a lightweigh ...
- 要是VISUAL STUDIO 2015带这些功能就好了
visual studio 2015 正式版立即就要出来了,事实上我原来满期待微软能出一套完美的移植的ANDROID和IOS应用的技术方案,这样WIN10正式版出来后,有一套比較好的移植框架,大家能够 ...
- Java反射,参数为数组
使用反射调用非公开的方法有时能解决许多问题,如果方法的参数是数组时类型该怎么传递呢?这里找到了一种方法记录一下 实例 比如: class A{ private void sayHello(String ...
- Spark学习笔记——构建分类模型
Spark中常见的三种分类模型:线性模型.决策树和朴素贝叶斯模型. 线性模型,简单而且相对容易扩展到非常大的数据集:线性模型又可以分成:1.逻辑回归:2.线性支持向量机 决策树是一个强大的非线性技术, ...
- 缓存技术PK:选择Memcached还是Redis?
缓存技术PK:选择Memcached还是Redis? memcached完全剖析----------------->高质量文章 memcached的最佳实践方案 数据缓存系统-memcached ...
- firadisk 把 win7(32位) 装入 VHD :仅仅支持内存模式:--mem
关键1:对于 win7(32位)来说,还可以在设备管理器内,通过添加“过时”硬件的方式导入wvblk驱动. 附件: grub4dos firadisk驱动
- [Bayes] qgamma & rgamma: Central Credible Interval
gamma分布的density的奇怪特性,如下: Poisson的Gamma先验 h(x) 的置信区间 的 获取 > n = > sumx= > > alpha= > ...