leetcode@ [68] Text Justification (String Manipulation)
2015-12-03 17:20
330 查看
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
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:
Return the formatted lines as:
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.
leetcode 68: 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 = 0; int i = 0, size = words.size(); while(i < size) { if(len + words[i].length() == maxWidth) { len = 0; res.push_back(words[i]); ++i; } else if(len + words[i].length() < maxWidth) { len = len + words[i].length(); int left = i; while(i+1 < words.size() && len+1+words[i+1].length() <= maxWidth) { len += 1+words[i+1].length(); ++i; } len = 0; int right = i, tot_len = 0, 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 - 2) { for(int k = left; k <= right - 1; ++k) { if(remain_empty_len > 0) { tmp = func(each_empty_len+1); 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 - 1; ++k) { tmp = func(1); 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
相关文章推荐
- nagios 被监控端一键配置脚本
- 【译】Android:更好的自定义字体方案
- 2015杭电新生赛1004质方数【打素数表】
- 关于创新的思考
- case when使用
- 统计学的七大支柱[转载@谢益辉]
- 响应式布局初始设置
- Ajax 无刷新上传文件插件 uploadify 的使用
- 六、虚函数表
- 2015杭电新生赛1002投币洗衣机【水模拟】
- 测试邮箱配置语句
- C++primer plus第六版课后编程练习答案13.1
- Unity光照贴图
- [1]-i.MX6Q OpenCL 学习-i.MX6Q介绍
- 改变Nim 标准库中streams 模块里的readInt 系列过程
- iOS 事件处理机制与图像渲染过程
- IOS-- UIView 基本属性用法
- 高斯模糊的算法
- Valid Parentheses 有效括号
- 这个是往字符串中加图片