美文网首页
Leetcode-Easy 806. Number of Lin

Leetcode-Easy 806. Number of Lin

作者: 致Great | 来源:发表于2018-08-29 11:47 被阅读20次

题目描述

给一个字符串S,从左到右将它们排列行,每行最大长度为100,同时给定一个数组withds,widths[0]对应着 a的宽度, widths[1]对应着b的宽度, ..., widths[25] 对应着z的宽度。
求:至少需要多少行以及最后一行的长度
下面是一个实例:

Example :
Input: 
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"
Output: [3, 60]
Explanation: 
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.

260的宽度,需要排成2个100的行,第3行的长度为60,所以结果是[3,60]

思路

逐个排列S中的每个字母,每排一个字母,需要检查当前行长度是否大于100,大于100,行数加1,长度变成最后一个元素的宽度。

代码实现

class Solution:
    def numberOfLines(self, widths, S):
        """
        :type widths: List[int]
        :type S: str
        :rtype: List[int]
        """
        letter_list=list("abcdefghijklmnopqrstuvwxyz")
        
        length=0
        line=1
        
        for s in S:
            length+=widths[letter_list.index(s)]
            if length>100:
                line+=1
                length=widths[letter_list.index(s)]
                
        return [line,length]

相关文章

  • Leetcode-Easy 806. Number of Lin

    题目描述 给一个字符串S,从左到右将它们排列行,每行最大长度为100,同时给定一个数组withds,widths[...

  • LeetCode 806. Number of Lines To

    We are to write the letters of a given string S, from lef...

  • 806. Number of Lines To Write St

    题目地址:https://leetcode.com/problems/number-of-lines-to-wri...

  • 【LeetCode】806. 写字符串需要的行数(Number

    题目如下:(题目链接戳我) 我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我...

  • 806. 缘

    梦你的时候那些不请自来的忧伤为何搅扰心绪可那只是在昏睡中 太阳升起的时候那些逍遥天地的红光用双手使劲去抓可只老了孤...

  • 【Lin】

  • lin

    我认为人与人交往讲究诚信,我认为朋友与朋友交谈,不用勾心斗角,我认为喜欢就是喜欢,无可厚非,我认为…… 事情没...

  • lin

    沿着银鳞 常常钻出水面的地方 一直走 走到银粼 常常推波水面的地方 一直走 走到银淋满整个夜空 捏起夜空的嘴角 笑...

  • Lin

    初恋是初三谈的。刚确定关系的第二天他就公诸与众,请一众好友喝汽水,这好像是那会我们学校的习俗。这几乎是我唯一一段正...

  • 2017.10.16感恩日记

    806.感恩早晨下的小雨,感恩小雨滋润大地,感恩小雨滋养空气。 807.感恩我形成我生物钟,可以到时间自然醒来。 ...

网友评论

      本文标题:Leetcode-Easy 806. Number of Lin

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