class Solution(object):
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
L =len(S)
wides = 0
n = 1
for i in range(L):
wides += widths[ord(S[i]) - 97]
while wides > 100:
wides = widths[ord(S[i]) - 97]
n += 1
return [n, wides]
leetcode - 806 - 写字符串需要的行数
最新推荐文章于 2023-04-04 13:46:12 发布
本文介绍了一种使用Python计算给定字符串在特定字体下的宽度的算法,该算法通过遍历字符串并累加每个字符的宽度来确定总宽度,同时考虑了换行条件。
278

被折叠的 条评论
为什么被折叠?



