【leetcode】Text Justification

本文探讨了如何高效地将字符串数组排列成指定宽度的文本块,并实现复杂对齐逻辑,包括空格平均分配和左对齐剩余空格补齐。

Question

Q
我觉得还是翻译一下好
给一个字符串数组,一个L整数
1、一行最多有L个字符
2、把这些字符串尽可能多得放在一行
3、每个字符串之间都有空格分开
4、字符串之间空格的个数平均分配,如果不能除尽,那么左边的空格数大于右边的
5、最后一行字符串左对齐,之间以一个空格分隔,剩余用空格补齐

Tips

No way, just let it be complex : )

Code

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> res;
        string tmp;
        int vSize = words.size();
        if(vSize==0)
            res.push_back(string(maxWidth,' '));
        else{
            if(words[0].length()==0){       //words[0]=""的时候
                res.push_back(string(maxWidth,' '));
                return res;
            }
        }

        int pos=0;  //当前已寻找过的字符串位置
        while(pos<vSize){
            tmp.clear();
            int count=0;        //计算出当前位置之后可以有几个字符串添加到本行
            int len=0;          //计算可以添加到本行的字符串长度
            bool flag = false;  //用来区别最后一位是“ ”(未完成),和已完成状态
            while(true){
                if(flag){
                    //cout<<tmp<<endl;
                    res.push_back(tmp);
                    break;
                }
                if(pos+count==vSize){       //最后一行
                    for(int i=0;i<count;++i){
                        tmp+=words[pos+i]+" ";
                    }
                    tmp+=string(maxWidth-tmp.length(),' ');
                    flag = true;
                }else if((len+=words[pos+count].length())>maxWidth){    //当前字符串不能用在其中
                    int blankCount = maxWidth, restBlank = 0;
                    for(int i=0;i<count;++i)
                        blankCount-=words[pos+i].length();
                    if(count!=1){
                        restBlank = blankCount%(count-1);       //除不尽
                    //cout<<"restBlank:"<<restBlank<<endl;
                        blankCount=blankCount/(count-1);            //每个间隔必须存在的空格数
                    }
                    string blank(blankCount,' ');
                    //cout<<'-'<<blank<<'-'<<endl;      //这里空格字符串的初始化有问题
                    //cout<<"blankCount:"<<blankCount<<endl
                    //  <<"blank length:"<<blank.length()<<endl;
                    for(int i=0;i<count;++i){
                        if(restBlank == 0)
                            tmp+=words[pos+i]+blank;
                        else{
                            tmp+=words[pos+i]+blank+" ";
                            --restBlank;
                        }
                        //cout<<"tmp:-"<<tmp<<'-'<<endl;
                    }
                    if(count!=1)
                        tmp.erase(tmp.end()-blankCount,tmp.end());
                    //cout<<tmp<<endl;
                    len = maxWidth;
                    flag = true;
                }else if(len==maxWidth){        //正好一个单词一个空地填满
                    string blank=" ";
                    for(int i=0;i<=count;++i)
                        tmp+=words[pos+i]+blank;
                    tmp.erase(tmp.end()-1,tmp.end());
                    len = maxWidth;
                    flag = true;
                    ++count;
                }else{
                    len+=1;         //表示暂时加一个‘ ’
                    ++count;
                }
            }
            pos = pos+count;
        }

        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值