149_leetcode_Text Justification

来源:互联网 发布:手机阻拦广告软件 编辑:程序博客网 时间:2024/05/16 12:32

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.

1:注意特殊情况;2:最后一行左对其,并且单词与单词之间只有一个空格;非最后一行,单词左对其和右对其,并且单词与单词之间的空格尽可能均匀, 如果只有一个单词的话,则是左对其;3:获得可以组成一行的单词,并对这一行单词进行相应的处理。

    vector<string> fullJustify(vector<string> &words, int L)    {        vector<string> result;                if(words.size() == 0)        {            return result;        }                int wordLength = 0;        int count = -1;        int size = (int)words.size();        vector<string> lineWords;                for(int i = 0; i < size; i++)        {            if(count + 1 + words[i].length() > L)            {                result.push_back(normal_line(lineWords, L, wordLength));                                lineWords.clear();                count = -1;                wordLength = 0;            }                        count = count + 1 + (int)words[i].length();            wordLength += words[i].length();            lineWords.push_back(words[i]);        }                result.push_back(end_line(lineWords, L, wordLength));        return result;    }        string normal_line(vector<string>& lineWords, int L, int wordLength)    {        string tmp = "";        int wordNumber = (int)lineWords.size();                if(wordNumber == 1)        {            tmp.append(lineWords[0]);            for(int i = 0; i < L - wordLength; i++)            {                tmp.append(" ");            }            return tmp;        }                int even_space = (L - wordLength) / (wordNumber - 1);        int left_space = (L - wordLength) % (wordNumber - 1);        tmp.append(lineWords[0]);                for(int i = 1; i < wordNumber; i++)        {            for(int j = 0; j < even_space; j++)            {                tmp.append(" ");            }            if(left_space > 0)            {                tmp.append(" ");                left_space--;            }                        tmp.append(lineWords[i]);        }        return tmp;    }        string end_line(vector<string>& lineWords, int L, int wordLength)    {        string tmp = "";        tmp.append(lineWords[0]);        int size = (int)lineWords.size();                for(int i = 1; i < size; i++)        {            tmp.append(" ");            tmp.append(lineWords[i]);        }                for(int i = 0; i < L - wordLength - size + 1; i++)        {            tmp.append(" ");        }                return tmp;    }


0 0
原创粉丝点击