Text Justification
来源:互联网 发布:simulink中in端口 编辑:程序博客网 时间:2024/05/12 00:56
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.
- 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 L) { // Start typing your C/C++ solution below // DO NOT write int main() function int space = 0;int i, j;vector<string> ans;string line;int size = words.size();int len = 0;int k;if(size == 1){line = words[0];line += string(L - words[0].size(), ' ');ans.push_back(line);return ans;}for(i = 0; i < size; i++){len = 0;line = "";for(j = i; j < size; j++){if(len + words[j].size() > L){space = L + 1 - len;break;} else if(len + words[j].size() == L){space = 0;break;}len += words[j].size();len += 1;}if(j == size){for(k = i; k < j - 1; k++){line+=words[k];line+=" ";}line+=words[k];line += string(L - line.size(), ' ');ans.push_back(line);break;} else if(space == 0){for(k = i; k < j; k++){line+=words[k];line+=" ";}line+=words[k];ans.push_back(line);i = j;} else {if(j - 1 == i){line += words[i];line += string(L - words[i].size(), ' ');ans.push_back(line);} else {int c = space /(j - 1 - i);int d = space % (j - 1 - i);for(k = i; k < j - 1; k++){line += words[k];if(k - i < d){line += string(c + 2, ' ');} else {line += string(c + 1, ' ');}}line+=words[k];ans.push_back(line);}i = j - 1;}}return ans; }};
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Text Justification
- Simplify Path
- java平台包
- 对 Linux 新手有用的 20 个命令
- oracle vm virtualbox 如何压缩虚拟机文件
- doc.install.webqq.to.pidgin.@ubuntu12.04
- Text Justification
- vcs+deubssy/verdi
- 对中级 Linux 用户有用的 20 个命令
- windows7最简单最快速解决“此windows副本不是正版”(“This copy of Windows is not genuine”)方法
- 垃圾回收的优点和原理,并考虑2种回收机制
- seo 网站结构优化及页面优化
- Word Search
- 菜鸟的学习之路(2) —java 运算符、语句和简单理解面向对象程序设计
- 堆排序