本题注意点:字符转数值的处理,或者说ASCII码
import numpy as np
def numberOfLines(widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
cur = 0
lines = 1
for i in range(len(S)):
index = ord(S[i])-ord('a')
if widths[index] <= 100-cur:
cur += widths[index]
else:
cur = widths[index]
lines += 1
return [lines, cur]
# sample:
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"
print(numberOfLines(widths, S))