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

    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.

    class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L) 
        {
            vector<string> result;
            int index=0;
            while(index<words.size())
            {
                vector<string> line;
                //find line words
                int len=0;
                while(len==0 || (len>0 && words[index].length()+1+len<=L))
                {
                    if(line.size()!=0)
                    {
                        line.push_back(" ");
                        len++;
                    }
                    len=len+words[index].length();
                    line.push_back(words[index]);
                    
                    index++;
                    if(index==words.size()) break;
                }            
                //only 1 word
                if(line.size()==1)
                {
                        string word=line[0];
                        for(int i=0;i<L-line[0].length();i++) word=word+" ";
                        result.push_back(word);
                }
                //more words
                else
                {
                    if(index!=words.size())
                    {
                        int i=1;
                        while(len<L)
                        {
                            line[i]=line[i]+" ";
                            len++;
                            //find a space
                            i=i+2;
                            if(i>=line.size()) i=1
                        }
                    }
                    else
                    {
                        int i=line.size()-1;
                        while(len<L)
                        {
                            line[i]=line[i]+" ";
                            len++;
                        }
                    }
                    string strline;
                    for(int i=0;i<line.size();i++)
                        strline=strline+line[i];
                    result.push_back(strline);
                }            
            }
            return result;
        }
    };
  • 相关阅读:
    CMake学习笔记
    右键添加"在此处打开命令窗口"菜单
    设置默认python模块源
    添加到附加组
    Unity宏处理
    挂载windows共享文件夹
    MacOS长按无效问题
    中文Locale
    笔记本用作无线路由器
    C# Winfrom iTextSharp 导出pdf 带二维码 表格嵌套 简单Dome
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759471.html
Copyright © 2020-2023  润新知