leetcode 68. Text Justification

来源:互联网 发布:科比生涯数据图 编辑:程序博客网 时间:2024/06/05 09:32

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

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 max)     {          vector<string> result;        vector<int> row;    //存每一行存的单词数目        vector<int> kong;   //每一行所有单词占的位置        row.push_back(0);          kong.push_back(0);        int count = 0; //存每一行中已经占用的位置(包括单词和空格)        for(int i = 0; i < words.size(); i++)        {            if(count == 0 && words[i].size() <= max)  //这一行没有东西            {                  row[row.size()-1]++;                kong[kong.size()-1] += words[i].size();                count = words[i].size();            }            else if(count != 0 && words[i].size() + count + 1 <= max) //这一行有单词 但还能放下            {                row[row.size()-1]++;                kong[kong.size()-1] += words[i].size();                count += words[i].size() + 1;            }            else //需要新的一行            {                row.push_back(0);                kong.push_back(0);                count=0;                i--;            }        }                string m1 = ""; //每一行的缓存        int k = 0; //k是单词索引        int kongge;        int duoyu;        for(int i = 0; i < row.size() - 1; i++)          {            kongge = max - kong[i]; //空格数目            if(row[i] > 1)                {                        duoyu = kongge % (row[i] - 1);  //多余的空格                kongge = kongge / (row[i] - 1); //每个单词后面跟的空格数                row[i] = row[i] + k;            //每一行的单词到row[i]就结束                while (k < row[i] && k < words.size())                {                    m1 = m1 + words[k];                     if (k != row[i]-1)                         for (int j = 0; j < kongge; j++)                            m1 = m1+ " ";                                      if (duoyu > 0)                    {                        m1 = m1 + " ";                        duoyu--;                    }                    k++;                }            }            else //这一行就一个单词            {                m1 = words[k];                 k++;                 for(int j = 0; j < kongge; j++)                     m1 = m1 + " ";            }            result.push_back(m1);            m1.clear();        }                //最后一行        while(k < words.size())        {           m1 = m1 + words[k];            if(k != words.size()-1)                m1 = m1 + " ";           k++;        }        for (int h = 0; h < max - kong[kong.size()-1] + 1 - row[row.size()-1]; h++)            m1 = m1 + " ";        result.push_back(m1);               return result;    }};


原创粉丝点击