Leetcode: Text Justification
来源:互联网 发布:网络博客娱乐平台出租 编辑:程序博客网 时间:2024/06/05 17:13
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 exactlyL 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.
题目很长,不难,就是需要注意细节。
class Solution {public: vector<string> fullJustify(vector<string> &words, int L) { int length = 0; vector<string> tmp; vector<string> result; for (int i = 0; i < words.size(); ++i) { int left_len = L - length; if (left_len < 0 || left_len < words[i].size()) { // reimburse the last space added ++left_len; string line; if (tmp.size() == 1) { line = tmp[0]; line.append(left_len, ' '); } else { // have added one space between words int avg = left_len / (tmp.size() - 1) + 1; int remainder = left_len % (tmp.size() - 1); for (int j = 0; j < tmp.size() - 1; ++j) { line += tmp[j]; line.append(avg, ' '); if (remainder-- > 0) { line.append(1, ' '); } } line += tmp.back(); } result.push_back(line); tmp.clear(); length = 0; // handle this word again --i; } else { tmp.push_back(words[i]); length += words[i].size() + 1; } } if (!tmp.empty()) { string line; for (int j = 0; j < tmp.size() - 1; ++j) { line += tmp[j] + ' '; } line += tmp.back(); line.append(L - line.size(), ' '); result.push_back(line); } return result; }};
- LeetCode : Text Justification
- [LeetCode] Text Justification
- leetcode 103: Text Justification
- [LeetCode]Text Justification
- [leetcode] Text Justification
- Text Justification leetcode
- Leetcode: Text Justification
- leetcode Text Justification
- LeetCode Text Justification
- LeetCode | Text Justification
- LeetCode Text Justification
- [Leetcode] Text Justification (Java)
- LeetCode OJ:Text Justification
- Leetcode: Text Justification
- [LeetCode] Text Justification
- 【LeetCode】Text Justification
- Text Justification -- LeetCode
- LeetCode: Text Justification [068]
- 第十七天dbutils的使用------Commons DbUtils(Apache)第三方的:只是对JDBC编码进行了简单的封装
- FusionCharts导出图表常见问题(FAQ)汇总---FusionCharts常见问题大全
- Hardcoded string,should use @string resource警告 && eclipse中一次性导入用到的类 && 重命名多处调用的对象
- 基于视图的NSTableView的更改选择的颜色 -
- What is PHP Composer
- Leetcode: Text Justification
- Android国际化资源文件自动化生成工具
- FusionCharts生成Flash图表常见问题FAQ
- 归一化图像
- IOS-录音功能
- Singleton模式
- 让你无处逃逸—无线定位技术简介
- Cocos2dx之Box2D物理引擎
- MFC 消息映射