[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 ...
随机推荐
- The Water Problem(排序)
The Water Problem Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
- 开源 免费 java CMS - FreeCMS1.9 简历管理
项目地址:http://code.google.com/p/freecms/ 简历管理 管理当前管理网站的简历数据. 1. 回复简历 选择须要回复的简历.然后点击"回复". 注意: ...
- Java SSH框架学习(入门)
SSH就是 struts+spring+hibernate 的一个集成框架,是java中一种流行的JAVA WEB 应用程序开源框架.由于我最熟悉的还是ASP.NET的的MVC和Python的Djan ...
- C. Table Decorations(Codeforces Round 273)
C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...
- AJAX - 创建 XMLHttpRequest 对象
XMLHttpRequest 是 AJAX 的基础. XMLHttpRequest 对象 所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject ...
- C#导出Word或Excel文件总显示Html标记
原因:Word或Excel文件包含的GridView没有查询到数据.
- Hibernate session.saveOrUpdate()方法
saveOrUpdate()方法同时包含了save()与update()方法的功能, 如果传入的参数是临时对象,就调用save()方法: 如果传入的参数是游离对象,就调用update()方法: 如果传 ...
- MySQL varchar和char类型
varchar和char是两种最主要的字符串类型.不幸的是,很难精确地解释这些值是怎么储存在磁盘和内存中的,因为这根存储引擎的具体实现有关.下面的描述假设使用的存储引擎是InnoDB或者MyISAM. ...
- No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)
In Build Settings are: Architectures: Starndard (armv7, armv7s) Base SDK: Latest iOS (iOS 6.0) Build ...