806. Number of Lines To Write String

题目地址: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()方法



所有题目解题方法和答案代码地址:https://github.com/fredfeng0326/LeetCode
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容