LeetCode | 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 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.

click to show corner cases.

Corner Cases:

  • 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.

思路:

考虑几个问题: 1)单词太长,跨行 2)最后一行的对齐方式不同 3)空格的分布

代码:

class Solution {
public:
    vector<string> result;
    queue<string> line;
    int count;
    vector<string> fullJustify(vector<string> &words, int L) {
        count=0;
        
        for(int i=0;i<words.size();i++)
        {
            if(line.empty()&&words[i].size()>=L)
            {
                string tmp;
                int j=0;
                for(;j<L;j++)
                {
                    tmp.push_back(words[i][j]);
                }
                result.push_back(tmp);
                if(j<L)
                {
                    tmp="";
                    for(;j<L;j++)
                    {
                        tmp.push_back(words[i][j]);
                    }
                    count+=tmp.size();
                    line.push(tmp);
                }
            }
            else
            {
                if(count+line.size()+words[i].size()<=L)
                {
                    count+=words[i].size();
                    line.push(words[i]);
                }
                else
                {
                    printOut(L);
                    count=0;
                    i--;
                }
            }
        }
        printOut(L, false);
        return result;
    }
    
    void printOut(int L, bool centerAlign=true)
    {
        if(!line.empty())
        {
            int more = L-count;
            string tmp;
            while(!line.empty())
            {
                tmp+=line.front();
                if(centerAlign)
                {
                    int each = more;
                    if(line.size()>1)
                    {
                        each = more/(line.size()-1)+(more%(line.size()-1)==0?0:1);
                    }
                    for(int k=0;k<each;k++)
                    {
                        tmp.push_back(' ');
                    }
                    more-=each;
                }
                else
                {
                    tmp.push_back(' ');
                    more--;
                }
                line.pop();
            };
            if(!centerAlign)
            {
                for(int k=0;k<more;k++)
                {
                    tmp.push_back(' ');
                }
            }
            result.push_back(tmp);
        }
    }
};

    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/17596141
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞