【LeetCode】806. Number of Lines To Write String 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/number-of-lines-to-write-string/description/
题目描述
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].
题目大意
有张纸,每行的长度为100.然后要在上面写字符串S,26个英文字符的每个字符的宽度右widths给出。如果一行写不下了,那么就往下一行写,看最后需要多少行,并且计算最后一行用了的长度。
解题方法
使用ASIIC码求长度
我们使用遍历S的方式去做,并用last统计这行写了多少宽度了,如果宽度大于100,那么就要lines+=1,last = width了,因为要换行。
代码:
class Solution(object):
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
lines = 1
last = 0
for s in S:
width = widths[ord(s) - ord('a')]
last += width
if last > 100:
lines += 1
last = width
return [lines, last]
使用字典保存长度
完全可以使用字典保存每个字符的长度,这样的话就能直接查找每个字符的长度了。
class Solution(object):
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
lines, row = 1, 0
lendict = {c : widths[i] for i, c in enumerate("abcdefghijklmnopqrstuvwxyz")}
N = len(S)
for s in S:
if row + lendict[s] > 100:
row = lendict[s]
lines += 1
else:
row += lendict[s]
return lines, row
日期
2018 年 4 月 3 日 —— 北京这天气,昨天穿短袖,今天穿棉袄
2018 年 11 月 6 日 —— 腰酸背痛要废了
【LeetCode】806. Number of Lines To Write String 解题报告(Python)的更多相关文章
- 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 ...
- 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 ...
随机推荐
- 模拟串口UART的实现
我所祷告的,就是要你们的爱心,在知识和见识上,多而又多,使你们能分辨是非,做诚实无过的人,直到基督的日子.--腓立比书[1:9~10] 最近在调的MCU的型号为STM32F030,配置芯片相较之前的M ...
- centos 7的命令变化
1.service -> systemctl命令 2.ifconfig -> ip 命令 3.netstat -> ss命令 4.route -> ip route命令 5.t ...
- postgresql安装部署
一.下载安装: 1.下载: 官网下载地址:https://www.postgresql.org/download/linux/redhat/ 也可以用这个:https://www.enterprise ...
- Spark基础:(一)初识Spark
1.Spark中的Python和Scala的Shell (1): Python的Spark Shell 也就是我们常说的PySpark Shell进入我们的Spark目录中然后输入 bin/pyspa ...
- Java【常用的日期操作】
目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...
- IntentFilter,PendingIntent
1.当Intent在组件间传递时,组件如果想告知Android系统自己能够响应那些Intent,那么就需要用到IntentFilter对象. IntentFilter对象负责过滤掉组件无法响应和处理的 ...
- go 代理
环境变量中设置 #GO111MODULE=auto GOPROXY=https://goproxy.io 如果不第一次,则在命令行设置 go env -w GO111MODULE=on go env ...
- Hibernate 总结(转)
JMX:Java Management Extensions.JCA: J2EE Contector ArchitectureJNDI: Java Namind and Directory Inter ...
- SpringBoot中使用JUnit4(入门篇)
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- C语言实现鼠标绘图
使用C语言+EGE图形库(Easy Graphics Engine).思路是通过不断绘制直线来实现鼠标绘图的功能,前一个时刻鼠标的坐标作为直线的起点,现在时刻的坐标作为终点(严格意义是线段而不是直线) ...