149_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.

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值