這是leetcode contest 77的新題:
第一次參加contest,前邊兩道題確實簡單。
version 1:
class Solution(object):
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
letters = dict(zip('abcdefghijklmnopqrstuvwxyz',range(26)))
lines = 1
last_line = 0
sum = 0
for i in S:
if sum + widths[letters[i]] <= 100:
sum = sum + widths[letters[i]]
else:
lines += 1
sum = widths[letters[i]]
last_line = sum
return [lines,last_line]
主要是dict和zip的用法,比較省事。