【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 ...
随机推荐
- C语言中的指针的小标可以是负数
首先,创建一个正常的数组 int A[20];.然后用指针指向其中间的元素 int *A2 = &(A[10]); 这样,A2[-10 ... 9] 就是一个可用的有效范围了. 1 2 3 4 ...
- 生产环境高可用centos7 安装配置RocketMQ-双主双从-同步双写(2m-2s-sync)
添加hosts信息[四台机器] vim /etc/hosts 192.168.119.130 rocketmq-nameserver1 192.168.119.130 rocketmq-master1 ...
- Docker学习(一)——安装docker
Suse12上安装docker 对于suse13.2之后的版本,因为docker已经被添加到了suse仓库中,直接使用sudo zypper install docker即可. suse12不 ...
- sax方式解析XML学习笔记
原理:对文档进行顺序扫描,当扫描到文档(document)开始与结束,元素开始与结束.文档结束等地方 通知事件处理函数,由事件处理函数相应动作然后继续同样的扫描,直至文档结束. 优点:消耗资源比较少: ...
- mysql之join浅析
1.可以使用join吗?使用join有什么问题呢?-- >超过3个表不使用join,笛卡尔积问题 -->这些问题是怎么造成的呢? 如果可以使用 Index Nested-Loop Join ...
- 查看IP访问量的shell脚本汇总
第一部分,1,查看TCP连接状态 netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ {++S[ ...
- Mybatis中 SIMPLE、REUSE、BATCH的区别
Executor分成两大类,一类是CacheExecutor,另一类是普通Executor. 普通类又分为: ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情.它为每个语句的执行 ...
- IDEA 使用rest client测试
一.进入 rest 控制台 idea 导航栏 ==> Tools ==> HTTP Client ==> Test RESTFUL Web Service 如图: 一般来说,idea ...
- SVM中的软间隔最大化与硬间隔最大化
参考文献:https://blog.csdn.net/Dominic_S/article/details/83002153 1.硬间隔最大化 对于以上的KKT条件可以看出,对于任意的训练样本总有ai= ...
- jdk1.8帮助文档中文可搜索
jdk1.8帮助文档中文可搜索 链接:https://pan.baidu.com/s/11beeZLpEIhciOd14WkCpdg 提取码:t4lw