https://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."] 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.

class Solution {
public:
string func(int len) {
string res = "";
while(len--) res += " ";
return res;
}
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> res; res.clear(); //Assuming that the length of longest string in the words must be shorter or equal to maxWidth.
int len = ;
int i = , size = words.size();
while(i < size) {
if(len + words[i].length() == maxWidth) {
len = ;
res.push_back(words[i]);
++i;
}
else if(len + words[i].length() < maxWidth) {
len = len + words[i].length();
int left = i;
while(i+ < words.size() && len++words[i+].length() <= maxWidth) {
len += +words[i+].length();
++i;
}
len = ;
int right = i, tot_len = , tot_empty_len, each_empty_len, remain_empty_len; for(int k = left;k <= right; ++k) tot_len += words[k].length(); tot_empty_len = maxWidth - tot_len;
each_empty_len = (left == right)? tot_empty_len: tot_empty_len / (right-left);
remain_empty_len = tot_empty_len - each_empty_len * (right-left); string r = "";
if(left == right) {
string tmp = func(tot_empty_len);
r += words[left] + tmp;
res.push_back(r);
}
else {
string tmp;
if(right <= size - ) {
for(int k = left; k <= right - ; ++k) {
if(remain_empty_len > ) {
tmp = func(each_empty_len+);
remain_empty_len--;
}
else tmp = func(each_empty_len);
r += words[k] + tmp;
}
r += words[right];
res.push_back(r);
}
else {
for(int k = left; k <= right - ; ++k) {
tmp = func();
r += words[k] + tmp;
}
r += words[right];
if(r.length() < maxWidth) r += func(maxWidth - r.length());
res.push_back(r);
}
}
++i;
}
}// end while return res;
}
};

leetcode 68: Text Justification

leetcode@ [68] Text Justification (String Manipulation)的更多相关文章

  1. [LeetCode] 68. Text Justification 文本对齐

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

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

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

  3. Leetcode#68 Text Justification

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

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

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

  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 length L, format the text such that each line has exactly L charac ...

  7. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  8. 【leetcode】Text Justification

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

  9. 【leetcode】Text Justification(hard) ☆

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

随机推荐

  1. 线索二叉树Threaded binary tree

    摘要   按照某种遍历方式对二叉树进行遍历,可以把二叉树中所有结点排序为一个线性序列.在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结点.这 ...

  2. Maven的Dependency怎么找?

    用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖(Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称 ...

  3. HDU 1422 重温世界杯(DP)

    点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...

  4. poj 2888 Magic Bracelet

    经典的有限制条件的Burnside计数+矩阵乘法!!! 对于这种限制条件的情况我们可以通过矩阵连乘得到,先初始化矩阵array[i][j]为1.如果颜色a和颜色b不能涂在相邻的珠子, 那么array[ ...

  5. poj 2454 Jersey Politics 随机化

    随机化算法+贪心! 将3*k排序后分成3分,将第二第三份的和分别加起来,让和与500*k比较,都大于则输出,否则,随机生成2个数,在第二第三份中交换! 代码如下: #include<iostre ...

  6. log4j:ERROR A "org.jboss.logging.appender.FileAppender" object is not assignable to a "org.apache.lo .

    log4j:ERROR A "org.jboss.logging.appender.FileAppender" object is not assignable to a &quo ...

  7. Linux Shell常用命令手册(Updating)

    检查远程端口是否对bash开放: nc -nvv $IP $PORT telnet $IP $PORT 当前任务的前后台切换: Ctrl + z fg 截取变量前5个字符: ${variable:0: ...

  8. Extension Method[下篇]

    四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...

  9. Unity3D集成SVN进行版本控制

    首先,AssetServer确实很好用,Unity内部集成的管理界面,操作很简单,提交冲突的后还可以进行文件比对.但学习使用过程中,发现文件体积较大的项目文件目录(600M),我提交不上去,会返回没有 ...

  10. Java内省

    什么是内省? Java语言对bean类属性.事件的一种缺省处理方法,例如类A中有属性name,那我们可以通过getName,setName来得到其值或者设置新的值. 什么是JavaBean? Java ...