[LeetCode]题解(python):068-Text Justification
题目来源:
https://leetcode.com/problems/text-justification/
题意分析:
输入一个字符串数组和一个规定长度L。将这个字符串数组的元素尽可能放到长度的L的字符串中,数组中的字符串不能拆开,一个长度L的字符串包括的若干个字符串之间用相等的空格间隔开。比如:
words: ["This", "is", "an", "example", "of", "text", "justification."],L: 16.
将返回
[
"This is an",
"example of text",
"justification. "
]
题目思路:
这道题目直接模拟解就可以了,要注意的是有很多细节要处理。从开始加入元素,长度增加,然后空格+1后继续加入新元素,直到长度大于L。然后判断空格的个数就可以了。
代码(Python):
class Solution(object):
def fullJustify(self, words, maxWidth):
"""
:type words: List[str]
:type maxWidth: int
:rtype: List[str]
"""
ans = []
i = 0
while i < len(words):
size,begin = 0,i
while i < len(words):
if size == 0:
newsize = len(words[i])
else:
newsize = size + len(words[i]) + 1
if newsize <= maxWidth:
size = newsize
else:
break
i += 1
s = maxWidth - size
if i - begin - 1 > 0 and i < len(words):
ns = s / (i - begin - 1)
s %= i - begin - 1
else:
ns = 0
j = begin
while j < i:
if j == begin: tmp = words[j]
else:
tmp += ' '*(ns + 1)
if s > 0 and i < len(words):
tmp += ' '
s -= 1
tmp += words[j]
j += 1
tmp += ' '*s
ans.append(tmp)
return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/5045245.html
[LeetCode]题解(python):068-Text Justification的更多相关文章
- Java for LeetCode 068 Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- LeetCode(68) Text Justification
题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Text Justification leetcode java
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【leetcode刷题笔记】Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- Android版xx助手之天天酷跑外挂具体分析
Android版xx助手之天天酷跑外挂具体分析 图/文 莫灰灰 背景 近些年来,移动互联网的大肆崛起,潜移默化中影响着人们的生活和工作习惯.当腾讯的微信平台接入手机游戏之后,移动端的游戏也開 ...
- 你应该知道CSS选择器技巧
什么是:before和:after? 该如何使用他们? :before是css中的一种伪元素,可用于在某个元素之前插入某些内容. :after是css中的一种伪元素,可用于在某个元素之后插入某些内容. ...
- HDU 5584 LCM Walk(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...
- A - A
A - A Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- BZOJ 1212: [HNOI2004]L语言( dp + trie )
因为单词很短...用trie然后每次dp暴力查找...用哈希+dp应该也是可以的.... ------------------------------------------------------- ...
- BZOJ 2302: [HAOI2011]Problem c( dp )
dp(i, j)表示从i~N中为j个人选定的方案数, 状态转移就考虑选多少人为i编号, 然后从i+1的方案数算过来就可以了. 时间复杂度O(TN^2) ------------------------ ...
- codeforces 631C. Report
题目链接 按题目给出的r, 维护一个递减的数列,然后在末尾补一个0. 比如样例给出的 4 21 2 4 32 31 2 递减的数列就是3 2 0, 操作的时候, 先变[3, 2), 然后变[2, 0) ...
- querySelector $() getElementBy区别
参考 http://stackoverflow.com/questions/14377590/queryselector-and-queryselectorall-vs-getelementsbycl ...
- 了解常见的浏览器内核 Trident,Geckos,Presto,Webkit
了解常见的浏览器内核 Trident,Geckos,Presto,Webkit 内核只是一个通俗的说法,英文名称为"Layout engine",翻译过来就是"排版引擎& ...
- png图片压缩优化
1.2 软件环境 软件名称:Opting下载地址: http://optipng.sourceforge.net/ 安装版本:0.7.5安装位置:/apps/svr/opting 安装可能遇到的问题: ...