68.文本格式化
来源:互联网 发布:淘宝店铺点击图片链接 编辑:程序博客网 时间:2024/06/10 16:52
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. "]
参考答案(c++):
class Solution {public: vector<string> fullJustify(vector<string> &words, int L) { vector<string> ans; int begin = 0; while (begin < words.size()) { int last = begin; int linesize = words[begin++].size(); while (begin < words.size() && linesize + 1 + words[begin].size() <= L) { linesize += 1 + words[begin].size(); begin++; } int spaces = 1, extra = 0; if (begin < words.size() && begin != last + 1) { spaces = (L - linesize) / (begin - last - 1) + 1; extra = (L - linesize) % (begin - last - 1); } ans.push_back(words[last++]); while (extra--) { ans.back().append(spaces+1, ' '); ans.back().append(words[last++]); } while (last < begin) { ans.back().append(spaces, ' '); ans.back().append(words[last++]); } ans.back().append(L-ans.back().size(), ' '); } return ans; }};
性能:
- 68.文本格式化
- 格式化文本
- 格式化文本
- 格式化文本
- 格式化文本
- 文本格式化
- 文本格式化
- WPF之格式化文本
- HTML 文本格式化实例
- 6 格式化文本
- java格式化文本输入
- 格式化文本信息
- HTML文本格式化
- HTML文本格式化
- 格式化文本框文本
- HTML 文本格式化
- C#格式化文本
- HTML 文本格式化
- django(三)---Template(2)
- css垂直水平居中方法
- 三种常见的排序算法
- QT子窗口收到父窗口发来的信号
- Oracle里面的用户user无法登录 LOCKED(TIMED)
- 68.文本格式化
- Winccflexable触摸屏的报警
- 用face_recognition写一个简单的人脸识别分类的程序
- 数据结构常用技巧
- 【PDF下载】双11技术峰会之揭秘阿里虚拟互动实验室
- D09 MapReduce程序运行过程原理
- 三国游戏 洛谷p1199
- list
- 关于2017电子设计国赛四旋翼飞行器题目的经验总结与教训