原题地址: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."]
L16.

Return the formatted lines as:

[
"This is an",
"example of text",
"justification. "
]

Note: Each word is guaranteed not to exceed L in length.

click to show corner cases.

Corner Cases:

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

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

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

  2. LeetCode:Text Justification

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

  3. LeetCode: Text Justification 解题报告

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  4. [Leetcode] 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 words显示的排序控制

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

  6. [LeetCode]题解(python):068-Text Justification

    题目来源: https://leetcode.com/problems/text-justification/ 题意分析: 输入一个字符串数组和一个规定长度L.将这个字符串数组的元素尽可能放到长度的L ...

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

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

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

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

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

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

随机推荐

  1. win7 配置DNS

    Network 右键 properties

  2. faker php测试数据库生成

    官方地址:https://github.com/fzaninotto/Faker 使用方式: 1.composer直接下载: composer require fzaninotto/faker 2.将 ...

  3. android shareUID

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 安卓中 全局进程,其他应用可以通过 分享UID 方式 和它 在同一个进程中.

  4. NOI.AC NOIP模拟赛 第三场 补记

    NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...

  5. 有向图强连通分量的Tarjan算法和Kosaraju算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  6. STL中优先队列的使用

    普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...

  7. Linux学习笔记05—文件与目录权限

    1. 绝对路径与相对路径绝对路径:路径的写法一定由根目录 ‘/’写起,例如 /usr/local/mysql 这就是绝对路径相对路径:路径的写法不是由根目录 ‘/’写起,例如:首先用户进入到/, 然后 ...

  8. 机器学习算法(5):卷积神经网络原理及其keras实现

    1.原理 CNN的资料特别多,这里不再赘述,仅收集相关的资料供大家参考: a.Deep learning:五十一(CNN的反向求导及练习) b.Deep Learning 2.实现 我们使用keras ...

  9. HNA CloudOS | 容器云服务专家

    HNA CloudOS | 容器云服务专家 http://cloudos.hnaresearch.com  

  10. WAP2.0(XHTML MP)基础介绍

    (一)XHTML MP 介绍XHTML MP(eXtensible HyperText Markup Language Mobile Profile)WAP2.0与WCSS(WAP CSS /WAP ...