【Leetcode】806. Number of Lines To Write String

這是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的用法,比較省事。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章