博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 68. Text Justification 文本对齐
阅读量:5321 次
发布时间:2019-06-14

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

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 Lcharacters.

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.

将由单词组成的数组,按每行最多L个字符进行对齐调整。

解法:先找到某一行能放下的单词,单词的字母数之和加上单词之间的空格要小于等于L,然后需要补充空格的话在填进去空格。假设有n个单词,那么就会有n-1个间隔,每个间隔的空格数应该是:(L-此行所有单词的长度和)/(n-1)。只用一个单词的话,排好单词在补上空格即可。

参考:

Java:

public List
fullJustify(String[] words, int L) { List
res = new ArrayList<>(); int n = words.length; char[] spaces = new char[L]; Arrays.fill(spaces, ' '); for(int i=0; i
0) sb.append(" "); sb.append(words[++j]); } } res.add(sb.toString()); } return res; }  

Python:

class Solution(object):    def fullJustify(self, words, maxWidth):        """        :type words: List[str]        :type maxWidth: int        :rtype: List[str]        """        def addSpaces(i, spaceCnt, maxWidth, is_last):            if i < spaceCnt:                # For the last line of text, it should be left justified,                # and no extra space is inserted between words.                return 1 if is_last else (maxWidth // spaceCnt) + int(i < maxWidth % spaceCnt)            return 0        def connect(words, maxWidth, begin, end, length, is_last):            s = []  # The extra space O(k) is spent here.            n = end - begin            for i in xrange(n):                s += words[begin + i],                s += ' ' * addSpaces(i, n - 1, maxWidth - length, is_last),            # For only one word in a line.            line = "".join(s)            if len(line) < maxWidth:                line += ' ' * (maxWidth - len(line))            return line        res = []        begin, length = 0, 0        for i in xrange(len(words)):            if length + len(words[i]) + (i - begin) > maxWidth:                res += connect(words, maxWidth, begin, i, length, False),                begin, length = i, 0            length += len(words[i])        # Last line.        res += connect(words, maxWidth, begin, len(words), length, True),        return res  

C++:

class Solution {  public:      vector
fullJustify(vector
& words, int maxWidth) { int cnt = 0, left = 0; vector
result; for(int i =0; i< words.size(); i++) { cnt += words[i].size(); if(cnt+i-left > maxWidth || i+1==words.size()) { if(cnt+i-left > maxWidth) cnt -= words[i--].size(); string str = words[left]; for(int j = left+1; j<= i; j++) { int m = maxWidth-cnt, n = i-left; if(i+1==words.size()) str += " "; else str.append(m/n + (j-left-1

  

 

 

转载于:https://www.cnblogs.com/lightwindy/p/8587072.html

你可能感兴趣的文章
ECMAscript 学习笔记(02)
查看>>
7z压缩gopath的src的批处理
查看>>
BZOJ2904
查看>>
CF576E
查看>>
【转载】计算机程序的思维逻辑 (5) - 小数计算为什么会出错?
查看>>
12、第七 - 网络编程基础 - 线程中的信号量(Semaphore)
查看>>
Linux Mysql 自动备份
查看>>
[转]MySQL远程连接ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'(111) 的问题
查看>>
[基础] 常见分布
查看>>
安装eclipse和CDT
查看>>
浅谈对象的序列化(Serialize)
查看>>
IIS 状态代码
查看>>
iOS 简单获取当前地理坐标
查看>>
第四周 兴趣问题清单
查看>>
279. Perfect Squares
查看>>
汇编学习笔记(2)-80x86寻址方式
查看>>
队列同步器详解
查看>>
Vim常用操作总结
查看>>
我的前端规范——HTML篇
查看>>
java操作MongoDB
查看>>