LeetCode - Text Justification 题解
来源:互联网 发布:软件项目计划书 编辑:程序博客网 时间:2024/05/21 15:05
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.
- 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> ans; vector<string> fullJustify(vector<string> &words, int L) { int i = 0; while(i < words.size()){ int now = words[i].length(), j = i + 1; while(j < words.size() && now + words[j].length() + 1 <= L){ now += words[j].length() + 1; j++; } string row; int num = j - i; now = now - num + 1; int left = L - now; if(num == 1){ row = words[i]; for(int k = 0; k < left; k++) row += " "; }else if(j == words.size()){ left -= num - 1; for(int k = i; k < j - 1; k++){ row += words[k]; row += " "; } row += words[j - 1]; for(int k = 0; k < left; k++) row += " "; }else{ int c = left / (num - 1); int r = left - c * (num - 1); for(int k = i; k < i + r; k++){ row += words[k]; for(int o = 1; o <= c + 1; o++) row += " "; } for(int k = i + r; k < j - 1; k++){ row += words[k]; for(int o =1; o <=c; o++) row += " "; } row += words[j - 1]; } i = j; ans.push_back(row); } if(ans.size() > 0){ string last = ans[ans.size() - 1]; } return ans; }};
- LeetCode - Text Justification 题解
- LeetCode 题解(103): Text Justification
- 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
- POJ 2406--Power Strings(kmp算法)
- 实验四
- hdu 5104 Primes Problem(数学)
- Objective-C基础笔记(4)Category
- JAVA的反射机制与动态代理
- LeetCode - Text Justification 题解
- ASP.NET动态网站开发培训-24.论文管理系统(四、制作Login页面和后台论文管理主页)
- 2014.11.15
- 学习资料(一)
- U3d学习第一天---界面布局更改及基本操作
- iOS 笔试题
- 3 - IOS之cocos3d - 2 - 把blender导出的3D文件加载到cocos3d
- C++初始化列表和大括号里构造的区别
- unity之第一节课和地形