leetcode 806. 寫字符串需要的行數

class Solution(object):
    def numberOfLines(self, widths, S):
        """
        :type widths: List[int]
        :type S: str
        :rtype: List[int]
        """
        base=ord('a')      #字母a的作爲基數,計算偏移量
        lines=1            #初始時有一行
        count=0
        for index in range(len(S)-1):
            aph=S[index]   #取字母
            pianyi=ord(aph)-base
            count+=widths[pianyi]
            if count+widths[ord(S[index+1])-base] >100:   #試探下一個,如果超過就進行接下里的操作
                count=0
                lines+=1
        count+=widths[ord(S[len(S)-1])-base]
        return [lines,count]

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