LeetCode806

 本題注意點:字符轉數值的處理,或者說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))

 

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