题目来源:

  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. 值得赞扬的尝试与进步——CSDN开源夏令营第一印象

    注:写这篇文章时我并未參加CSDN开源夏令营,也不确定是否会參加以及是否能參加上. 欣闻CSDN举办了"CSDN开源夏令营"活动.第一感觉是CSDN作为活动的组织者是很值得称赞的. ...

  2. k8s 集群基本概念

    一.概述: kubernetes是google开源的容器集群管理系统,提供应用部署.维护.扩展机制等功能,利用kubernetes能方便管理跨集群运行容器化的应用,简称:k8s(k与s之间有8个字母) ...

  3. JQ工具函数运用

    1.把对象转换为字符串 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  4. 如何在MFC对话框之间自定义消息传递

    在MFC项目开发中,涉及到不同模块间数据信息的传递,如用户在登录界面成功登录后向系统管理模块发送用户名和密码等信息. 首先,需明确以下两点: 谁要发送这个消息--消息发送方 谁要接受这个消息--消息接 ...

  5. JavaScript引用类型之Object类型

    在JavaScript中大多数的引用类型都是Object的实例,Object类型也是使用最多的类型! 创建Object类型实例的方式有两种,下面分别来分析一下: (1)第一种是使用new操作符后跟Ob ...

  6. 关于消息推送和service的一些调查-清理内存通知栏点击无响应

    起因:做了两个带推送的app:HiApp和WeApp,前者个推,后者百度推送,但前者有一个小缺陷. 现象:两部手机 1.htcD820t手机,运行中的app利用自带的关闭最近程序后,通知栏不清理该ap ...

  7. android SDK和ADT的更新

    ADT版本较低时,造成xml布局文件无法预览,更新时发现google被墙,找网上的招数暂且解决了问题. 1. SDK的更新: 将https://修改为http:// 修改方法是:在Android SD ...

  8. win7(32 bit) 环境下点击打印预览报错解决办法

      如题,报错截图如下   :     解决办法如下: 1.关闭系统数据执行保护.具体操作: 在win7 系统命令行中执行如下命令: bcdedit.exe /set {current} nx Alw ...

  9. 前自加(++a)与后自加(a++)的差别

    自加是自己加1的操作.比如a++ 是a+1 ,变量a变成了 a+1的值. 如果是简单的只做 a++:或者++a的语句,效果是一样的. 但是如果嵌入到复杂的语句中,比如 b = a++: 和 b = + ...

  10. Fedora 19下Guacamole的安装使用

    由于我要使用RDP实现web远程桌面,因此需要用到了Guacamole这个开源的软件.之前用Ubuntu12.04折腾了一晚上,也没有找到依赖库文件,而Guacamole的官方安装说明却没有介绍这个依 ...