题目来源:

  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. Effective C++ 条款11

    在operator=中处理"自我赋值" 什么是自我赋值,非常明显. 就是自己的值赋值给了自己.以下的代码就是自我赋值: class Widget { public: Widget& ...

  2. 深入理解Linux网络技术内幕——中断与网络驱动程序

    接收到帧时通知驱动程序     在网络环境中.设备(网卡)接收到一个数据帧时,须要通知驱动程序进行处理. 有一下几种通知机制: 轮询:     内核不断检查设备是否有话要说.(比較耗资源,但在一些情况 ...

  3. md笔记——使用 @font-face 引入你喜欢的字体

    使用 @font-face 引入你喜欢的字体 原理 CSS3的自定义字体@font-face 规则的工作原理 使用@font-face规则初看起来非常简单.从本质上看,它只需要两个步骤. 首先,使用 ...

  4. JavaScript引用类型之Array数组的concat()和push()方法的区别

    在javascript中,我们一般都只用push向数组的尾部插入新元素的,但是其实在javascript中还有另外一个方法和push一样,也是向数组尾部插入新元素的,但是他们之间却存在着一定的区别,当 ...

  5. 表A中有两个表示时间的字段A,B;如果B的值大于A的值,则把B的值更新为A的值

    sql语句:表A中有两个表示时间的字段A,B:如果B的值大于A的值,则把B的值更新为A的值 update 表名 set B=A where B>A

  6. iOS 常用第三方

    MWPhotoBrowser 非常好用的图片浏览器 FDFullscreenPopGesture 用于全屏滑动切换视图 Aspects 用于快速AOP编程 AFNetworking iOS开发中最为火 ...

  7. Mvc--Html.ActionLink()用法

    },new{ target="_blank"})会生成 <a href="Products/Detail/1" target="_blank&q ...

  8. ping时不知道ping那个Ip的解决办法

    利用命令:nslookup Windows+R键,输入CMD,输入命令nslookup www.baidu.com OK!这得在联网情况下,如果本身nslookup就不行的话,可不可以认为网络就不好使 ...

  9. rsa 密钥和公钥的生成

    openssl genrsa -out prikey.pem openssl rsa -in prikey.pem -pubout -out pubkey.pem

  10. IIS上不能播放mp4

    iis不支持mp4格式,需要手动添加. 进入iis服务管理器,打开你的网站,然后点击MIME类型---添加(扩展名:mp4   MIME类型:application/octet-stream) 如此即 ...