LeetCode OJ Text Justification
来源:互联网 发布:mysql存储图片 编辑:程序博客网 时间:2024/06/10 17:41
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 Lcharacters.
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.
notice that we should just submit the codes of the class.
class Solution { //solution: //try insert as many as words in a line(here a line means a L string of an element of the vector) //insert blank to the linepublic: vector<string> fullJustify(vector<string> &words, int L) { vector<string> ans; // the answer vector vector<string> a_line; // every line of the answer int pos = 0; // the position of an element of the input while (pos < words.size()) { a_line.clear(); a_line.push_back(words[pos]); // there will be a least an element in a line int word_num = 1; // used to store the number of the element in a line int sum_word_length = words[pos].size(); // the length of all the elements in a line int sum_line_length = words[pos++].size(); // the length of all the elements in a line including blanks while (1) { if ((pos >= words.size()) || (sum_line_length + words[pos].size() + 1 > L)) { // if a blank and an element added to the line will exceed the max_length, break break; } else { // else insert the element and a blank before the element word_num++; sum_word_length += words[pos].size(); sum_line_length += words[pos].size() + 1; a_line.push_back(words[pos++]); } } if (pos == words.size()) { // if the elements in the input is run out of just right, we don't need to consider the blanks problem(it is the last line) string line_str = a_line[0]; for (int i = 1; i < a_line.size(); i++) { line_str += " " + a_line[i]; } for (int i = line_str.size(); i < L; i++) { line_str += " "; } ans.push_back(line_str); break; } string line_str = a_line[0]; string blank; // the blank, may one may more int evenly_blank_num; // each blank's min_length int remind_blank_num; // the remaining blanks if (word_num == 1) { // if there is only a word in a line for (int i = line_str.size(); i < L; i++) { line_str += " "; } } else { evenly_blank_num = (L - sum_word_length) / (word_num - 1); remind_blank_num = (L - sum_word_length) % (word_num - 1); for (int i = 0; i < evenly_blank_num; i++) { blank += " "; } for (int i = 1; i < a_line.size(); i++) { line_str += blank; if (remind_blank_num) { // notice that the remainder is smaller than the divisor alsolutely, we just use this way to add the blanks line_str += " "; remind_blank_num--; } line_str += a_line[i]; } } ans.push_back(line_str); } return ans; }};
- LeetCode OJ:Text Justification
- LeetCode OJ - Text Justification
- LeetCode OJ Text Justification
- LeetCode OJ算法题(六十八):Text Justification
- LeetCode OJ 之 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: Text Justification
- linux 下实线文件切割--split
- LeetCode OJ Remove Duplicates from Sorted List II
- 如何让基于对话框的MFC程序一运行起来就完全隐藏
- LeetCode OJ 3Sum Closest
- LeetCode OJ Evaluate Reverse Polish Notation
- LeetCode OJ Text Justification
- hdu 1548 A strange lift(bfs)
- (大数据之hadoop)hadoop2.2.0的伪分布式安装说明
- LeetCode OJ Reverse Words in a String
- 机器人技术—基于DELPHI的数据采集与分析类上位机软件的编写
- Xcode下使用SVN教程
- Chat-online
- UVA - 12532 Interval Product
- UVA 10130 SuperSale (DP 01背包)