68. 文本左右对齐

class Solution {
    public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int lastWordsIndex=-1,//上轮循环处理的最后一个word
        wordsTotalNow = 0,//当前循环统计的words数量
        charsTotalNow=0,//当前循环统计的char数量
        curLastWordIndex=0,//当前循环处理最后一个words 的index
        curWordIndex = 0,
        emptyCharLen = 1,//每次填充空格数量
        extraMaxIndex = 0;//需要额外空格分界的最大index
        vector<string> ret;
        string temp;
        if(maxWidth<=0){
            return ret;
        }
        while (curWordIndex<words.size())
        {
            if(charsTotalNow+words[curWordIndex].length()>maxWidth){
                //调整emptyCharLen,计算额外空格数量及其分界,最后重新进入循环来填充新的空格数量
                curLastWordIndex = curWordIndex-1;//标记当前循环最后一个word的位置
                curWordIndex = lastWordsIndex;//令循环cursor回退至上次最后一个位置
                
                if(wordsTotalNow>1){
                    extraMaxIndex = (maxWidth-charsTotalNow + wordsTotalNow*emptyCharLen)%(wordsTotalNow-1);//emptyCharLen+1(0~extraMaxIndex-1)和emptyCharLen分界停止于extraMaxIndex
                    emptyCharLen = (maxWidth-charsTotalNow + wordsTotalNow*emptyCharLen)/(wordsTotalNow-1);//计算额外空格数量和不同数量的分界word index
                }else{
                    emptyCharLen = maxWidth-charsTotalNow+emptyCharLen;
                    extraMaxIndex = 0;
                }
                temp="";//重新开始构造字符串
                charsTotalNow = wordsTotalNow = 0;
            }else if(curWordIndex==words.size()-1){
                //最后一个,重新按照   emptyCharLen = 1 计算
                emptyCharLen = 1;
                temp = "";
                break;
            }else if(charsTotalNow+words[curWordIndex].length()==maxWidth){
                temp+=words[curWordIndex];
                ret.push_back(temp);
                temp = "";
                lastWordsIndex = curWordIndex;
                wordsTotalNow = charsTotalNow = curLastWordIndex = extraMaxIndex = 0;
                
                emptyCharLen = 1;
            }else{
                wordsTotalNow++;
                charsTotalNow += words[curWordIndex].length();

                temp+=words[curWordIndex];
                for (int i = 0; i < emptyCharLen; i++)
                {
                    temp+=" ";
                }
                charsTotalNow+=emptyCharLen;
                if(wordsTotalNow<=extraMaxIndex){
                    temp+=" ";
                    charsTotalNow+=1;
                }
                
                
                if(wordsTotalNow<=1 && (charsTotalNow==maxWidth)){
                    ret.push_back(temp);
                    temp = "";
                    lastWordsIndex = curWordIndex;
                    wordsTotalNow = charsTotalNow = curLastWordIndex = extraMaxIndex = 0;
                    
                    emptyCharLen = 1;
                }
                
            }
     
            

            curWordIndex++;
        }
            wordsTotalNow++;
            curWordIndex = lastWordsIndex+1;
            emptyCharLen = 1;
            while (curWordIndex<words.size() ||  temp.length()<maxWidth)
            {
                if(curWordIndex<words.size()){
                    temp += words[curWordIndex];
                    if(temp.length()<maxWidth){
                        temp += " ";
                    }
                    curWordIndex++;
                }else{
                    temp += " ";

                }
            }
            if(temp.length()>0){
                ret.push_back(temp);
            }

        return ret;
        
        
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值