博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Text Justification
阅读量:5751 次
发布时间:2019-06-18

本文共 3601 字,大约阅读时间需要 12 分钟。

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."]
L: 16.

Return the formatted lines as:

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

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

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.
 这题主要是对单词进行按行排序.主要要考虑的情况有三种:
1.提示中提醒的,一行只有一个单词,则放置在左边,left-justified.
2.最后一行,即使有多个单词,也是放置在左边,left-justified,多个单词时,之间只有一个空格.
3.不是最后一行,但是间隔无法整除时,左边要比右边间隔大,但是要尽量均分,即只能相差一个.
代码如下:
class Solution(object):    def fullJustify(self, words, maxWidth):        """        :type words: List[str]        :type maxWidth: int        :rtype: List[str]        """        #slice the words in a sequential way, don't understand the last line meaning         if not words:            return []        res = []        n = len(words)        start = 0        while start < n:            lengthSum = 0            cnt = 0            while start+cnt < n and  lengthSum + len(words[start+cnt]) + cnt <=  maxWidth: #frist test whether can add the word, then add the word in the line                lengthSum += len(words[start+cnt])                cnt += 1            #make the line            if cnt == 1:                res.append(words[start]+(maxWidth-lengthSum)*" ")            elif start + cnt == n:  #the last line, several words together, left justify                cur = " ".join(words[start: start+cnt])                if len(cur) < maxWidth:                    cur+= " "*(maxWidth-len(cur))                res.append(cur)            else: #common line                avg = (maxWidth - lengthSum)/(cnt-1)                if (maxWidth - lengthSum)%(cnt-1) == 0:                    res.append((" "*avg).join(words[start:start+cnt]))                else:                    n1 = maxWidth - lengthSum - avg*(cnt-1) + 1#number of left slots has more space                    n2 = avg - (cnt-1)                    cur =(" "*(avg+1)).join(words[start:start+n1])                    cur += " "*avg                    cur += (" "*avg).join(words[start+n1:start+cnt])                    res.append(cur)            start += cnt        return res

 参考leetcode精简思路,找长度一样,主要是生成行的时候简单:

vector
fullJustify(vector
&words, int L) { vector
res; for(int i = 0, k, l; i < words.size(); i += k) { for(k = l = 0; i + k < words.size() and l + words[i+k].size() <= L - k; k++) { l += words[i+k].size(); } string tmp = words[i]; for(int j = 0; j < k - 1; j++) { if(i + k >= words.size()) tmp += " "; else tmp += string((L - l) / (k - 1) + (j < (L - l) % (k - 1)), ' '); tmp += words[i+j+1]; } tmp += string(L - tmp.size(), ' '); res.push_back(tmp); } return res;}

 

 

转载于:https://www.cnblogs.com/sherylwang/p/5862922.html

你可能感兴趣的文章
后端技术精选 - 收藏集 - 掘金
查看>>
Laravel 服务容器
查看>>
mac安装kubernetes并运行echoserver
查看>>
多页架构的前后端分离方案(webpack+express)
查看>>
算法(第4版) Chapter 1
查看>>
前端技术选型的遗憾和经验教训
查看>>
“亲切照料”下的领域驱动设计
查看>>
SRE工程师到底是做什么的?
查看>>
解读:Red Hat为什么收购Ansible
查看>>
Ossim下的安全合规管理
查看>>
DelphiWebMVC框架下BPL热部署实现
查看>>
C++与MySQL的冲突
查看>>
siki学习之观察者模式笔记
查看>>
spring.net 继承
查看>>
ES6:模块简单解释
查看>>
JavaScript indexOf() 方法
查看>>
ZJU PAT 1023
查看>>
WMI远程访问问题解决方法
查看>>
Android开发历程_15(AppWidget的使用)
查看>>
阿花宝宝 Java 笔记 之 初识java
查看>>