[leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/
题意:
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.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.
Return the formatted lines as:
[
"This is an",
"example of text",
"justification. "
]
Note: Each word is guaranteed not to exceed L in length.
- A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
解题思路:这道题主要是要考虑的细节比较多,要编写正确不是很容易。
代码:
class Solution:
# @param words, a list of strings
# @param L, an integer
# @return a list of strings
def fullJustify(self, words, L):
res=[]
i=0
while i<len(words):
size=0; begin=i
while i<len(words):
newsize=len(words[i]) if size==0 else size+len(words[i])+1
if newsize<=L: size=newsize
else: break
i+=1
spaceCount=L-size
if i-begin-1>0 and i<len(words):
everyCount=spaceCount/(i-begin-1)
spaceCount%=i-begin-1
else:
everyCount=0
j=begin
while j<i:
if j==begin: s=words[j]
else:
s+=' '*(everyCount+1)
if spaceCount>0 and i<len(words):
s+=' '
spaceCount-=1
s+=words[j]
j+=1
s+=' '*spaceCount
res.append(s)
return res
[leetcode]Text Justification @ Python的更多相关文章
- [LeetCode] 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 chara ...
- LeetCode: Text Justification 解题报告
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- [Leetcode] 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 words显示的排序控制
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [LeetCode]题解(python):068-Text Justification
题目来源: https://leetcode.com/problems/text-justification/ 题意分析: 输入一个字符串数组和一个规定长度L.将这个字符串数组的元素尽可能放到长度的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@ [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 ...
随机推荐
- speech模块实现语音识别
1.pip安装speech.pywin32 pip install speech pip install pywin32 2.例子 #!/usr/bin/python # coding:utf-8 f ...
- hiho1270 建造基地([Offer收割]编程练习赛1)
题目意思很简单, 一道类似于背包的问题,问恰好超过背包容量的最小取值.并且需要计算n次,每计算一次都要将所有的物品的价值w[i] /= t,最后输出背包n次的总使用容量. 如果无法实现输出”No An ...
- 运维服务器手段(监控宝,Nagios,百度通告平台)
站在"巨人"的肩膀上运维 现实问题 之前在论坛看到一个运维工程师的帖子,内容如下: "现在的一个IT工作者最头疼的就是加班,秃顶的是IT工作者最多.单身的是IT工作者最多 ...
- Android的GridView控件点击图片变暗效果
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC ...
- Android笔记之属性动画
前言.动画分类 例如以下图所看到的,Android的动画主要分为三种: 以下首先说说 属性动画 所谓属性动画-- 就是指对象的属性值发生了变化,如控件位置和透明度等. 举例,如今要实现一个按键先下移. ...
- AngularJS的增删改查、state嵌套案例,不涉及服务端
本篇实践一个案例,大致是:左边有导航菜单,右边显示列表,并可对列表项编辑或删除,也可添加新的列表项.借此,可体会到:如何组织可扩展的AngualrJS文件结构,如何点击左侧菜单项右侧显示相应内容,an ...
- SpringMVC和Springboot的区别
转自站在浪潮之巅的原文SpringMVC和Springboot的区别(网摘) spring boot 我理解就是把 spring spring mvc spring data jpa 等等的一些常用的 ...
- 监测uitableview 向上滑动和向下滑动的事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat height = _varietyTableView.frame.si ...
- 打开IPHONE的sms.db短信文件 方法
先将导出的sms.db文件改名为sms.sqlite再下载个火狐浏览器<IGNORE_JS_OP style="WIDOWS: 2; TEXT-TRANSFORM: none; BAC ...
- C#编程(二十七)----------创建泛型类
创建泛型类 首先介绍一个一般的,非泛型的简化链表类,可以包含任意类型的对象,以后再把这个类转化为泛型类. 在立案表中,一个元素引用下一个元素.所以必须创建一个类,他将对象封装在链表中,并引用下一个对象 ...