题目来源:

  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的更多相关文章

  1. 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 ...

  2. LeetCode(68) Text Justification

    题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...

  3. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  4. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  5. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  7. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. Text Justification leetcode java

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  9. 【leetcode刷题笔记】Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. 单页web应用开发流程

    用循环的视角审视Web应用开发 框定一个一致的SPA图形用户界面(GUI)和模型 将SPA的原则带回服务器端 聚集于对合适的应用进行早期SPA开发[3]  SPA协调的起点是认识到SPA与脚本和网页编 ...

  2. Mac 上开启一个简单的服务器

    终端输入命令: python -m SimpleHTTPServer 会开启一个端口为8000的本地服务器.

  3. Cookie、Session

    Cookie.Session Cookie 保存在浏览器端. 4kb 只能保存字符串,还不能是中文. 获取:Request.getCookies(); 设置时间:setMaxAge(); 小于零是浏览 ...

  4. hive集群安装配置

    hive 是JAVA写的的一个数据仓库,依赖hadoop.没有安装hadoop的,请参考http://blog.csdn.net/lovemelovemycode/article/details/91 ...

  5. 页面加载时,页面中DIV随之滑动出来;去掉页面滚动条

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  6. 现在有m组n个有序数组,例如{1,2,3,4},{2,3,4,6},{1,3,5,7},在这些数组中选择第k小的数据,然后返回这个值

    问题描述:现在有m组n个有序数组,例如{1,2,3,4},{2,3,4,6},{1,3,5,7},在这些数组中选择第k小的数据,然后返回这个值 思路:参照两个数组归并的过程,每次选取最小的数据进行比较 ...

  7. CF 599D Spongebob and Squares(数学)

    题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...

  8. linux杂记(十二?) 关于账号和密码的二三事

    关于密码的二三事 关于账号和密码的二三事 久了不更linux的相关知识,实在是懒得想内容点(纯粹是懒).那么今天就来谈谈关于linux密码和账号的重要概念. 假如你的主机遭到入侵,那么对方的第一个侵入 ...

  9. less 工具

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  10. 一周学会Mootools 1.4中文教程:(6)动画

    先看一下动画的参数设置: 参数: fps - (number:默认是50) 每秒的帧数. unit - (string:默认是 false) 单位,可为 'px','em',或 '%'. link - ...