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.

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 {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int n = words.size(), l, extra, eachex, i, j, k, t;
vector<int> len(n, );
for(i=; i<n; i++)
len[i] = words[i].length();
vector<string> ans;
i=;
while(i<n)
{
for(l=len[i], j=i+; j<n && l<=maxWidth; j++)
l += len[j]+;
string s = words[i];
if(j==n && l<=maxWidth) //the last line
{
for(k = i+; k<j; k++)
{
s += ' ';
s += words[k];
}
}
else
{
j--;
l -= len[j]+;
extra = maxWidth-l;
eachex = (j>i+) ? extra / (j-i-) : extra;
for(k=i+; k<j; k++)
{
if(extra)
{
if((j == i) || (extra == eachex*(j-k)))
{
for(t=; t<eachex; t++)
s += ' ';
extra -= eachex;
}
else
{
for(t=; t<eachex+; t++)
s += ' ';
extra -= eachex+;
} }
s += ' ';
s += words[k];
}
}
for(k=s.length(); k<maxWidth; k++)
s += ' ';
ans.push_back(s);
i = j;
}
return ans;
}
};

测试用例:

["a","b","c","d","e"] 1 -- ["a","b","c","d","e"]

["Listen","to","many,","speak","to","a","few."] 6 -- ["Listen","to    ","many, ","speak ","to   a","few.  "]
["Here","is","an","example","of","text","justification."] 16 --

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

["Don't","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."]
30 --

["Don't  go  around  saying  the","world  owes  you a living; the","world owes you nothing; it was","here first.                   "]
 

68. Text Justification *HARD*的更多相关文章

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

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

  2. 68. Text Justification

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

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

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

  4. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  5. 【LeetCode】68. Text Justification

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

  6. 68. Text Justification一行单词 两端对齐

    [抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly  ...

  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. 68. Text Justification (JAVA)

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  9. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

随机推荐

  1. MySQL Crash Course #11# Chapter 20. Updating and Deleting Data

    INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...

  2. Mysql管理工具 SqlYog快捷键大全

    Ctrl+M   创建一个新的连接Ctrl+N   使用当前设置新建连接Ctrl+F4   断开当前连接 对象浏览器F5   刷新对象浏览器(默认)Ctrl+B   设置焦点于对象浏览器 SQL 窗口 ...

  3. lambda表达式Bug——修改捕获变量失败

    解<C++ Primer 5th>的 9-50 练习题时,遇到了 lambda表达式值捕获和引用捕获之区别问题. 欲修改捕获的变量 sum,累加之.但当时忘记值捕获和引用捕获是有区别的.下 ...

  4. 20145227鄢曼君《网络对抗》Web基础

    20145227鄢曼君<网络对抗>Web基础 实验内容 (1)Web前端HTML (2)Web前端javascipt (3)Web后端:MySQL基础:正常安装.启动MySQL,建库.创建 ...

  5. Java异常类复习总结

    个人理解先行: 异常类是当在程序出现问题时抛出的一个警告.提示你程序设计或者代码有存在错误的地方. 异常类和Error都继承自Throwable, Throwable继承自Object类. Runti ...

  6. C#学习笔记(十三):继承

    继承 object是引用类型 public:最高权限,公开的 Protected:外部不可以访问 Internal:类的默认访问是什么作用域 Private:类成员默认   基类实例:可以通过base ...

  7. Nlog、elasticsearch、Kibana以及logstash在项目中的应用(一)

    前言 最近在做文档管理中,需要记录每个管理员以及用户在使用过程中的所有操作记录,本来是通过EF直接将操作数据记录在数据库中,在查询的时候直接从数据库中读取,但是这样太蠢了,于是在网上找到了logsta ...

  8. 51nod 1445 变色DNA(最短路变形)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445 题意: 思路: 挺好的一道题目,如果$colormap[i][j] ...

  9. Java中的this

    首先this作为关键字其实是随着对象的创建而产生的,当我们调用对象的一个方法的时候: 例如: A a = new A(); a.f(1)  其实我们可以理解为a.f(a,1) 编译器默默的把所操作的对 ...

  10. Linux——系统开关机指令简单学习笔记

    关机: 命令名称:shutdown 命令所在路径:/usr/sbin/shutdown 执行权限:root 语法:shutdown 功能描述:关机 范例:# shutdown -h now 重启: 命 ...