leetcode 68. Text Justification
2016-06-29 17:16
183 查看
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ’ ’ when necessary so that each line has exactly L characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
给定一个单词数组,和每行的字符数,按照要求匹配
首先判断下每行:
当前tmp中存储的单词的长度flag+该单词长度<=maxWidth
如果满足,则将该单词加入到tmp中
如果不满足,tmp数组直接进行规范化(modify()函数)
注意到最后一段是按照正常的输出格式,少的部分进行空格补全
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ’ ’ when necessary so that each line has exactly L characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
给定一个单词数组,和每行的字符数,按照要求匹配
首先判断下每行:
当前tmp中存储的单词的长度flag+该单词长度<=maxWidth
如果满足,则将该单词加入到tmp中
如果不满足,tmp数组直接进行规范化(modify()函数)
注意到最后一段是按照正常的输出格式,少的部分进行空格补全
class Solution(object): def modify(self,tmpwords,maxWidth): """ :type tmpwords:List[str] eg:['This','is','an'] :type maxWidth: int """ word_count=len(tmpwords) space_count=max(1,word_count-1) character_num=sum([len(i) for i in tmpwords]) base_space=(maxWidth-character_num)/space_count extra_space=(maxWidth-character_num)%space_count ret='' for i in range(space_count): ret=ret+tmpwords[i]+''.join([' ']*base_space) if i<extra_space: ret=ret+' ' if word_count>1: ret=ret+tmpwords[-1] return ret def fullJustify(self, words, maxWidth): """ :type words: List[str] :type maxWidth: int :rtype: List[str] """ result=[] tmp=[] flag=0 for i in words: if flag+len(i)<=maxWidth: tmp.append(i) flag=flag+len(i)+1 else: if len(tmp): result.append(self.modify(tmp,maxWidth)) tmp=[i] flag=len(i)+1 ans='' for i in tmp[0:len(tmp)-1]: ans=ans+i+' ' if len(tmp): ans=ans+tmp[-1] ans=ans+''.join([' ']*(maxWidth-len(ans))) result.append(ans) return result
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解