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."]
L16.

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;     }};


0 0
原创粉丝点击