原题地址: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. android activity 启动模式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 1,标准的, 2,单个 顶部 3,单个 任务 4,单个 实例 标准的 就是 每启动一次这 ...

  2. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  3. 正睿OI 提高 Day1T3 ZYB玩字符串(DP)

    题目链接 设可能的答案串为p,长为len.p一定是s的一个子串且len|n. 虽然一些p在s中可能被断成若干段,但删掉其中的若干段后,这段区间一定会被全部消掉. 于是枚举p后,可以用f[i][j]表示 ...

  4. [USACO3.2]Sweet Butter

    题目大意: 给定一张$k$个结点,$m$条边的无向图,其中有$n$个点被标记,在这$k$个点中找出一个点使得这个点到那$n$个点的最短距离之和最小,求出这个距离和. 思路: 对于每个标记结点跑最短路, ...

  5. BZOJ3518 : 点组计数

    若直线的斜率为0或者不存在斜率,则有$nC(m,3)+mC(n,3)$种方案.若直线的斜率不为0,只需考虑斜率为正的情况,最后答案再乘以2即可.枚举两个点的坐标差,设$t=\min(n,m)$,则有: ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力

    D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...

  7. UVALive 6908 Electric Bike dp

    Electric Bike 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  8. CentOS 7下使用chkconfig添加的服务无法使用/etc/profile里面的环境变量

    经过分析/etc/profile为入口的,基本是登录后执行的变量,而使用chkconfig添加的服务多变以守护经常运行,没有登录. CentOS 7下使用chkconfig添加的服务无法使用/etc/ ...

  9. CentOS 7下KVM支持虚拟化/嵌套虚拟化配置

    开启虚拟化: cat << EOF > /etc/modprobe.d/kvm-nested.conf options kvm-intel nested=1 options kvm- ...

  10. STM32 TIMER REGISTER