leetcode 68. Text Justification

本文介绍了一种文本两端对齐的算法实现,该算法能够确保每行文本在给定长度下完全填充,同时尽可能均匀地分配额外空格。讨论了不同情况下的处理方式,如单词数量、空格分布及最后一行的特殊处理。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值