力扣68.文本左右对齐

题目:biubiu
题意: 给一个单词数组,然后给一个句子的长度,现在让你把这些单词组合成句子,在单词之间插入空格,最后一个句子是特殊情况,只有最后一个句子的最后是空格。
直接遍历就可以,然后就需要处理一些小细节,例如这个句子只有一个单词,然后就是空格的插入。

#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
class Solution {
    
    
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
    
    
        int x;
        string str;
        vector<string>ans;
        for (int i = 0; i < words.size(); i++) {
    
    
            x = 0;
            str.erase(0);
            for (int j = i; j < words.size(); j++) {
    
    
                x += words[j].size();
                if (x > maxWidth) {
    
    
                    x -= words[j].size();
                    x = maxWidth - x + (j - i);
                    int y;
                    int z;
                    if (j - i == 1) {
    
    ///只有一个单词的情况
                        str += words[i];
                        str.append(maxWidth - words[i].size(), ' ');
                        ans.push_back(str);
                        break;
                    }
                    else {
    
    
                        y = x / (j - i - 1);///如果空格可以均分,那么均分的空格
                        z = x % (j - i - 1);///无法均分的空格
                    }
                    for (int k = i; k < j; k++) {
    
    
                        str += words[k];
                        if (k == j - 1) {
    
    
                            i = k;
                            break;
                        }
                        str.append(y, ' ');
                        if (z) {
    
    ///题目要求空格不能均分的时候,左边比右边多,插入一个单词,
                            ///在插入y个空格之后,在插入一个多的空格,这样就保证左边比右边多
                            str.append(1, ' ');
                            z--;
                        }
                    }
                    ans.push_back(str);
                    break;
                }
                else {
    
    
                    if (j == words.size() - 1) {
    
    
                        for (int k = i; k <= j; k++) {
    
    
                            str += words[k];
                            str.append(1, ' ');
                        }
                        x = maxWidth - x - 1;
                        if(x>0)///存在小于0的情况,说明最后多了一个空格
                           str.append(x, ' ');
                        if (x <0)///最后一位多输出了一个空格
                            str.erase(maxWidth);
                        ans.push_back(str);
                        return ans;
                    }
                    x++;
                }
            }
        }
        return ans;
    }
};

int main() {
    
    
    vector<string>words;
    int x;
    while (1) {
    
    
        string str;
        cin >> str;
        words.push_back(str);
        if (cin.get() == '\n')
            break;
    }
    cin >> x;
    Solution s;
    words = s.fullJustify(words, x);
    for (int i = 0; i < words.size(); i++) {
    
    
        cout << words[i] << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43840681/article/details/121321111