字符串写入的行数
把字符串S中的字符从左到右写入行中。 每行最大宽度度为100,如果往后新写一个字符导致该行宽度超过100,则写入下一行。
注意:一个字符的宽度不为1!给定一个数组widths,其中widths[0]是字符a的宽度,widths[1]是字符b的宽度,…,widths[25]是字符’z’的宽度。
问:把S全部写完,至少需要多少行?最后一行用去的宽度是多少? 将结果作为长度为2的整数列表返回。
class Solution:
"""
@param widths: an array
@param S: a string
@return: how many lines have at least one character from S, and what is the width used by the last such line
"""
def numberOfLines(self, widths, S):
# Write your code here
result = [1, 0]
for i in S:
result[1] += widths[ord(i)-97]
if result[1] > 100:
result[0] += 1
result[1] = widths[ord(i)-97]
else:
continue
return result