leetcode 68:文本左右对齐

原理很简单,但是写起来比较复杂,首先将不超过maxWidth的字符串放入一个新的数组result,不要忽略空格所占空间

对result进行处理,首先去除字符串最后面的一个或多个空格,然后计算去除空格后的字符串大小,用过最大大小减去去除空格之后的字符串大小,得到d 用除以单词个数-1 即为原来每个空格位置需要插入的空格个数 

对于只有一个单词的字符串和最后一个字符串只需要在最后面补足够的空格即可

std::vector<std::string> fullJustify(std::vector<std::string>& words, int maxWidth) {
    std::vector<std::string> result;
    std::vector<std::string> d1;
    std::vector<std::vector<std::string>> d2;
    std::string s1="";
    int i=0;

    while(i!=words.size()) {
        s1="";
        d1.clear();
        while((words[i].size()+s1.size())<=maxWidth) {
            s1+=words[i];
            s1+=" ";
            d1.push_back(words[i]);
            i++;
        }
        d2.push_back(d1);
        result.push_back(s1);
    }

    std::string s2="";
    for(int i=0;i<result.size()-1;i++){
        s2="";
        result[i].erase(result[i].find_last_not_of(" ")+1);
        if(result[i].find_first_of(" ")==-1){
            int t1=result[i].size();
            for(int j=0;j<maxWidth-t1;j++)
            {
                result[i]+=" ";
            }
            continue;
        }

        int c=d2[i].size()-1;
        int d=maxWidth-result[i].size();
        int x1=d/c;
        int x2=d%c;

        s2=d2[i][0];
        for(int j=1;j<d2[i].size();j++)
        {
            if(j<x2+1){
                for(int k=0;k<x1+2;k++){
                    s2+=" ";
                }
            }
            else
            {
                for(int k=0;k<x1+1;k++){
                    s2+=" ";
                }
            }
            s2+=d2[i][j];
        }
        result[i]=s2;
    }
        
result[result.size()-1].erase(result[result.size()-1].find_last_not_of(" ")+1);
    int t=result[result.size()-1].size();
    int d=maxWidth-t;
    for(int i=0;i<d;i++)
        result[result.size()-1]+=" ";

    return result;
}

猜你喜欢

转载自blog.csdn.net/u013263891/article/details/84661231
今日推荐