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. servlet之一(概念/如何写/运行/错误提示/乱码处理)

    # 1. 什么是Servlet?   sun公司制订的一种用来扩展web服务器功能的组件规范. ## (1) 扩展web服务器功能(servlet的产生背景)       web服务器只能处理静态资源 ...

  2. 20165310 Java实验四 《Android程序设计》

    20165310 实验四 <Android程序设计> 第24章:初识Android 任务一:改写res目录中的内容,Hello World后要显示自己的学号,自己学号前后一名同学的学号 首 ...

  3. 判断一个String中是否有指定字符或字符串

    String test=“qwer”; if (test.contains("个we")){ do; }

  4. P3939 数颜色

    目录 题目 思路1(待修莫队) 思路2(vector+二分) 代码1 代码2 题目 P3939 数颜色 思路1(待修莫队) 哇,这不是莫队模板题吗 3e5,TLE45分 不行 我有信仰啊 pow(n, ...

  5. Wireshark过滤总结

    Wireshark提供了两种过滤器:捕获过滤器:在抓包之前就设定好过滤条件,然后只抓取符合条件的数据包.显示过滤器:在已捕获的数据包集合中设置过滤条件,隐藏不想显示的数据包,只显示符合条件的数据包.需 ...

  6. Windows 10 无法调节亮度的解决办法

    我的笔记本在更新完14393后开始的一段时间内是可以正常调节亮度的,但是后来就莫名奇妙地不能调节亮度了,而且电源选项里的亮度调节滚动条也不见了.网上查了很多方法,诸如修改注册表.更新驱动之类的都试过, ...

  7. python 获取本地语言和编码的代码

    #! /usr/bin/env python # encoding=utf8 import locale language, encoding = locale.getdefaultlocale() ...

  8. Spring/Spring MVC/Spring Boot的区别

    1.spring boot更简单,容易上手: 2.spring boot对第三方技术进行了很好的封装,提供了大量的第三方接口: 3.通过依赖配置,不需要XML等配置文件: 4.提供了安全性等特性. S ...

  9. VS2010_x86_编译错误

    1.两个头文件 相互include 报出来的错误,没有直接说是 嵌套include,而是这个现象: error: C4430: 缺少类型说明符 - 假定为 int.注意: C++ 不支持默认 int ...

  10. Java内存管理知识你需要吗?

    0.Java 对内存的划分: Java虚拟机规范将物理内存(主内存和CPU中的缓存.寄存器)划分为程序计数器.Java 虚拟机栈.本地方法栈.Java 堆.方法区五个区域,但并没有规定这些区域的具体实 ...