当前位置:网站首页>leetcode--806. 写字符串需要的行数
leetcode--806. 写字符串需要的行数
2022-04-22 17:47:00 【爱学习的Amelia】
-
题目:我们要把给定的字符串
S从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100个单位,那么我们应该把这个字母写到下一行。我们给定了一个数组widths,这个数组widths[0]代表'a'需要的单位,widths[1]代表'b'需要的单位,…,widths[25]代表'z'需要的单位。
现在回答两个问题:至少多少行能放下S,以及最后一行使用的宽度是多少个单位?将你的答案作为长度为2的整数列表返回。1 -
示例:
# 示例 1
输入:
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"
输出: [3, 60]
解释:
所有的字符拥有相同的占用单位10。所以书写所有的26个字母,
我们需要2个整行和占用60个单位的一行。
# 示例 2
输入:
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"
输出: [2, 4]
解释:
除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位.
最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。
所以,这个答案是2行,第二行有4个单位宽度。
- 提示:
- 字符串
S的长度在[1, 1000]的范围。 S只包含小写字母。widths是长度为26的数组。widths[i]值的范围在[2, 10]。
- 思路:
定义一个存储行数的值,设置一个循环,跳出条件为s为空则跳出
定义容量值为100,字符个数为0,以及a~z的列表
遍历循环s,将给定的值相减,若cnt值小于0则将刚刚减走的值加回来,观察发现字符个数要减1才合理,跳出本轮for循环
将s前面遍历过的字符都删掉,行数+1
返回行数以及100减去cnt的剩余容量
- 解法一:
class Solution:
def numberOfLines(self, widths, s):
lens = 0
while len(s) != 0:
lists = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z']
cnt, s_len = 100, 0
for i in s:
cnt -= widths[lists.index(i)]
s_len += 1
if cnt < 0:
cnt += widths[lists.index(i)]
s_len -= 1
break
s = s[s_len:]
lens += 1
return lens, 100-cnt
- 思路:
定义一个总和的值,行数,依次遍历s值总和等于给定值列表的字符的ascii编码减去a的ascii编码
若总和等于100,将总和重置,并将行数加1;否则综合大于100时,总和等于当前字符值,行数加1
若总和等于0,返回行数-1,使用容量值为100;否则,返回行数与总和
- 解法二:
class Solution:
def numberOfLines(self, widths, s):
s_sum = 0
row = 1
for c in s:
s_sum += widths[ord(c) - ord('a')]
if s_sum == 100:
s_sum = 0
row += 1
elif s_sum > 100:
s_sum = widths[ord(c) - ord('a')]
row += 1
if s_sum == 0:
return [row - 1, 100]
else:
return [row, s_sum]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-lines-to-write-string ︎
版权声明
本文为[爱学习的Amelia]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46361294/article/details/124322976
边栏推荐
- 【飞轮储能发电】基于simulink的飞轮和蓄电池在微网中的仿真
- Soft test high item notes | information system security
- idea设置services窗口显示树状服务
- L1-002 打印沙漏 (20 分)
- 微软测试人员简述
- Unable to translate SQLException with Error code ‘0‘, will now try the fallback translator
- 科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智
- 文章推荐 2022.4.18
- Cisp-pte SQL injection advanced level bypass bypass skills
- Inheritance relationship of Servlet
猜你喜欢

软考高项笔记 | PERT 三点估算

Multithreaded notes | future interface function

Activiti suspend and activate tasks

软考高项笔记 | 信息系统生命周期

多线程笔记 | Future 接口功能

S7-1500 specific methods and steps of data exchange between CPU and OPC UA server through OPC UA client
Android--sqlite

Knowing that it is inevitable to break the listing in Hong Kong: Zhou Yuan was "beaten in the face", and the growth is extremely dependent on marketing

每日一练-第一天-3.18

Fiddler- 篡改服务器返回的数据
随机推荐
Inheritance relationship of Servlet
期货怎么办理开户,国内期货开户哪里方便又安全?
软考高项笔记 | 运维服务分类
Soft test high item notes | contents of project proposal
Pushing hand of industrial Internet innovation iteration
[flywheel energy storage and power generation] simulation of flywheel and battery in microgrid based on Simulink
Knowing that it is inevitable to break the listing in Hong Kong: Zhou Yuan was "beaten in the face", and the growth is extremely dependent on marketing
耐看、好开被认可,ID. CROZZ一周年,新款产品力又变强?
pr导出的avi文件,如何才能被imageJ读read?
丘成桐已全职加入清华
Sunyuchen announced the launch of usdd to promote financial freedom
Soft test high-level notes | tools and techniques for collecting needs
锚定美元的USDD,如何引领数字货币变革
视频播放常用的库
find lower_bound upper_bound
软考高项笔记 | 立项管理内容
设计千万级学生管理系统的考试试卷存储方案
Nonlinear optimization problem three -- matlab
spaCy教程学习
It's not just what you see on the surface, but the product logic behind JetBlue Dasheng