806. Number of Lines To Write St

作者: fred_33c7 | 来源:发表于2018-06-27 15:32 被阅读0次

题目地址:https://leetcode.com/problems/number-of-lines-to-write-string/description/
大意:给定一个字符串,让他们分行书写,每个字符串长度给定,当本行书写到最后一个字符超过100的时候,这个字符得另起一行。

只要判断这行的总字符数是不是超过100,如果超过100,就把他置为当前字符的长度就行了。同时把行数加1,比较简单。

# 806. Number of Lines To Write String
class Solution:
   def numberOfLines(self, widths, S):
       """
       :type widths: List[int]
       :type S: str
       :rtype: List[int]
       """
       line_words = 0
       lines = 1
       for word in S:
           word_length = widths[ord(word)-97]
           line_words = word_length + line_words
           if line_words > 100:
               line_words = word_length
               lines +=1
           print (line_words)
       return [lines,line_words]

a = Solution()
print (a.numberOfLines([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],'abcdefghijklmnopqrstuvwxyz'))

知识点:

字母和ascii转换 =》 ord()方法

相关文章

网友评论

    本文标题:806. Number of Lines To Write St

    本文链接:https://www.haomeiwen.com/subject/amobyftx.html