68. 文本左右对齐

根据题解:
大体思路是判断几个单词放在一行中,即这几个单词加上其后面固定的空格的长度小于maxWidth
满足条件后,进入fillWords函数中进行字符串的处理。
其中,要注意最后一行的处理(左对齐)。
处理过程如下:(其实就是空格的处理)

  1. 计算单词数量wordCount
  2. 计算减去固定空格数量和单词长度的空格数量spaceCount
  3. 对于每个单词(除了最后一个单词),其后面一定有一个固定空格,然后就是平分剩下空格spaceCount。
    • 平分的过程:
    • 平均每个单词空隙分配spaceCount / (wordCount - 1)个空格
    • 多出来的空格(假设是spaceExtra)分配给前spaceExtra个空隙
  4. 对于只有一个单词 / 最后一行 两种情况的讨论
    • 最后一行的每个单词只有后面的固定空格,即spaceSuffix
    • 不进行剩余空格的平分过程
class Solution {
public:

    string fillWords(vector<string>& words, int bg, int ed, int maxWidth, bool lastLine = false) {
        int wordCount = ed - bg + 1;
        int spaceCount = maxWidth + 1 - wordCount;	// +1相当于抵消最后一个单词后面的空格
        for (int i = bg; i <= ed; ++i) {
            spaceCount -= words[i].size();
        }
        int spaceSuffix = 1;
        // 如果一行只有一个单词不用平分空格
        int spaceAvg = (wordCount == 1) ? 0 : spaceCount / (wordCount - 1);
        int spaceExtra = (wordCount == 1) ? 0 : spaceCount % (wordCount - 1);

        string ans;
        for (int i = bg; i < ed; ++i) { // 除了最后一个单词
            ans += words[i];
            if (lastLine) {
                fill_n(back_inserter(ans), 1, ' ');	// 最后一行,每遇到一个单词添加固定1个空格即可
                continue;
            }
            fill_n(back_inserter(ans), spaceSuffix + spaceAvg + ((i - bg) < spaceExtra), ' '); // 非最后一行
        }
        ans += words[ed]; // 添加最后一个单词
        fill_n(back_inserter(ans), maxWidth - ans.size(), ' '); // 一行只有一个单词/最后一行 要把右边的空格填补起来
        return ans;
    }

    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ans;
        int cnt = 0;
        int bg = 0;
        for (int i = 0; i < words.size(); ++i) {
            cnt += words[i].size() + 1;
            if (i + 1 == words.size() || cnt + words[i + 1].size() > maxWidth) {
                ans.push_back(fillWords(words, bg, i, maxWidth, i + 1 == words.size()));
                bg = i + 1;
                cnt = 0;
            }
        }
        return ans;
    }
};

其中,

  1. fill_n 是 C++ 中的一个算法函数,用于将指定数量的元素设置为特定的值。
    first:要填充的起始位置的迭代器。
    count:要填充的元素数量。
    value:要填充的值。
  2. back_inserter 是 C++ 标准库中的一个函数模板,位于 <iterator> 头文件中,用于创建一个插入迭代器,该迭代器可以用于在容器的末尾插入元素。back_inserter 函数接受一个容器 x,并返回一个插入迭代器,该迭代器会在容器 x 的末尾插入元素。
  • Java版本
class Solution {
    public String fillWords(String[] words, int bg, int ed, int maxWidth, boolean lastLine) {
        String ans = new String();
        int wordCount = ed - bg + 1;
        int spaceCount = maxWidth + 1 - wordCount;
        for (int i = bg; i <= ed; ++i) {
            spaceCount -= words[i].length();
        }
        int spaceSuffix = 1;
        int spaceAvg = (wordCount == 1) ? 0 : spaceCount / (wordCount - 1);
        int spaceExtra = (wordCount == 1) ? 0 : spaceCount % (wordCount - 1);
        for (int i = bg; i < ed; ++i) {
            ans += words[i];
            if (lastLine) {
                ans += ' ';
                continue;
            }
            for (int j = 0; j < spaceSuffix + spaceAvg + (((i - bg) < spaceExtra) ? 1 : 0); ++j) {
                ans += ' ';
            }
        }
        ans += words[ed];
        for (int j = ans.length(); j < maxWidth; ++j) {
            ans += ' ';
        }
        return ans;
    }

    public List<String> fullJustify(String[] words, int maxWidth) {
        List<String> ans = new ArrayList<> ();
        int cnt = 0;
        int bg = 0;
        for (int i = 0; i < words.length; ++i) {
            cnt += words[i].length() + 1;
            if (i + 1 == words.length || cnt + words[i + 1].length() > maxWidth) {
                ans.add(fillWords(words, bg, i, maxWidth, i + 1 == words.length));
                bg = i + 1;
                cnt = 0;
            }
        }
        return ans;
    }
}

注意:

  1. String[]的长度是.length,而String的长度是.length()
  2. 关于List和ArrayList的区别
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值