Text Justification
2015-07-11 21:08
337 查看
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
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:
L:
Return the formatted lines as:
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.
Solution:
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.
Solution:
class Solution { public: vector<string> fullJustify(vector<string>& words, int maxWidth) { vector<string> res; int num = 0, len = 0, start = 0; for(int i = 0; i < words.size(); ++i) { len += words[i].length(); string str = ""; if(len > maxWidth) { len -= words[i].length(); int sum = maxWidth - len + num; i--; if(num == 1) { str += words[start]; while(sum--) str += " "; } else { int span = sum / (num - 1); int extra = sum - span * (num - 1); string space = ""; while(span--) space += " "; for(int j = start; j < start + extra; ++j) str += words[j] + space + " "; for(int j = start + extra; j < i; ++j) str += words[j] + space; str += words[i]; } res.push_back(str); start = i + 1; len = num = 0; } else if(len == maxWidth) { for(int j = start; j < i; ++j) str += words[j] + " "; str += words[i]; res.push_back(str); start = i + 1; len = num = 0; } else { num++; len++; } } if(len <= maxWidth && num) { string str = ""; for(int j = start; j < words.size(); ++j) str += words[j] + " "; for(int j = 0; j < maxWidth - len; ++j) str += " "; res.push_back(str); } return res; } };
相关文章推荐
- 大约SQL现场“这包括”与“包括在”字符串的写法
- python 命令行参数解析
- 【啊哈!算法】算法6:只有五行的Floyd最短路算法
- java面向对象学习心得
- leetcode 日经贴,Cpp code -Lowest Common Ancestor of a Binary Search Tree
- 不要再挺着,我是我自己
- Android使用学习之绘图(Canvas,Paint)与手势感应及其应用(乒乓球小游戏)
- c++内存管理
- ubuntu下如何截图
- Java学习笔记03 俄罗斯方块界面
- 在nodeschool学习作用域链和闭包(Scope Chains And Closures)
- IJG JPEG 函数库:文件列表
- JAVA基础-字符串
- BZOJ 1192 鬼谷子的钱袋
- 求最大连续区间和的几种方法
- 暑假学习计划与学长的经验
- 疑问: IP数据包的服务类型字段TOS的作用是什么???
- NYOJ 242 计算球体积
- 1017. A除以B (20)
- HDU 4814 Golden Radio Base