leetcode@ [68] Text Justification (String Manipulation)
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.
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)的更多相关文章
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- [leetcode]68. Text Justification文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】68. Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【leetcode】Text Justification(hard) ☆
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- 我的vim配置
之前都在虚拟机下面捣鼓Linux,有种隔靴搔痒的感觉.为了更快地熟悉Linux系统,重新安装了Ubuntu,首先就是配置vim. 下面是我的vim配置,为了方便,我在代码后添加注释说明. 1.配置C/ ...
- 李洪强iOS开发之OC常见错误汇总
// // main.m // 16 - 常见错误汇总 // // Created by vic fan on 16/7/13. // Copyright © 2016年 李洪强. All r ...
- [itint5]字符串匹配
http://www.itint5.com/oj/#15 用hash来做,目前为止做到最好也是case16超时(20w的规模),即使分桶也超时.注意计算hashcode时,'a'要算成1,否则如果'a ...
- 使用Nginx+Keepalived组建高可用负载平衡Web server集群
一,首先说明一下网络拓扑结构: 1,Nginx 反向代理Server(HA): ①Nginx master:192.168.1.157 ②Nginx backup:192.168.1. ...
- Android:控件布局(相对布局)RelativeLayout
RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...
- A过的题目
1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...
- POJ2533——Longest Ordered Subsequence(简单的DP)
Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...
- xml格式化
Vim怎么格式化xml,完全不会,vim的缩进也搞不明白
- Windbg:如何给字符串下条件断点
因为Windgb支持MASM语法,字符串的比较方法有$scmp和$sicmp.用法和c中的字符串比较方法一致.在需要比较字符串成员变量的时候,遇到了点问题.因为字符串成员变量无法直接获取字符串内容.p ...
- 高难度(1)常用的AR构架或库
Layar http://www.layar.com/ Layar旨在打造的一个开放的增强现实的平台,任何第三方都可以通过Layar的开发接口来打造基于Layar的自己的增强现实应用. 高通AR开发包 ...