zl程序教程

您现在的位置是:首页 >  其他

当前栏目

LeetCode-1592. 重新排列单词间的空格【字符串】

LeetCode 字符串 单词 空格
2023-09-14 09:01:27 时间

题目描述:

给你一个字符串 text ,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 text 至少包含一个单词 。

请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 text 字符串的长度相等。

返回 重新排列空格后的字符串 。

示例 1:

输入:text = " this is a sentence "
输出:“this is a sentence”
解释:总共有 9 个空格和 4 个单词。可以将 9 个空格平均分配到相邻单词之间,相邻单词间空格数为:9 / (4-1) = 3 个。

示例 2:

输入:text = " practice makes perfect"
输出:"practice makes perfect "
解释:总共有 7 个空格和 3 个单词。7 / (3-1) = 3 个空格加上 1 个多余的空格。多余的空格需要放在字符串的末尾。

示例 3:

输入:text = “hello world”
输出:“hello world”

示例 4:

输入:text = " walks udp package into bar a"
输出:"walks udp package into bar a "

示例 5:

输入:text = “a”
输出:“a”

提示:

1 <= text.length <= 100
text 由小写英文字母和 ’ ’ 组成
text 中至少包含一个单词

解题思路一:模拟,简单题真的不简单,条件是真的多,容易出错。

class Solution {
public:
    vector<string_view> split(const string_view &str, char trim) {
        int n = str.size();
        vector<string_view> res;
        int pos = 0;
        while (pos < n) {
            while(pos < n && str[pos] == trim) {
                pos++;
            }
            if (pos < n) {
                int curr = pos;
                while(pos < n && str[pos] != trim) {
                    pos++;
                }
                res.emplace_back(str.substr(curr, pos - curr));
            }
        }
        return res;
    }

    string reorderSpaces(string text) {
        int length = text.size();
        vector<string_view> words = split(text, ' ');
        int cntSpace = length;
        int wordCount = 0;
        for (auto &word : words) {
            if (word.size() > 0) {
                cntSpace -= word.size();
                wordCount++;
            }
        }

        string ans;
        if (words.size() == 1) {
            ans.append(words[0]);
            for (int i = 0; i < cntSpace; i++) {
                ans.push_back(' ');
            }
            return ans;
        }
        int perSpace = cntSpace / (wordCount - 1);
        int restSpace = cntSpace % (wordCount - 1);
        for (int i = 0; i < words.size(); i++) {
            if (words[i].size() == 0) {
                continue;
            }
            if (ans.size() > 0) {
                for (int j = 0; j < perSpace; j++) {
                    ans.push_back(' ');
                }
            }
            ans.append(words[i]);
        }
        for (int i = 0; i < restSpace; i++) {
            ans.push_back(' ');
        }
        return ans;
    }
};

解题思路二:这个代码更短一点

class Solution {
public:
    string reorderSpaces(string text) {
        vector<string> vec;//用数组保存单词
        stringstream in(text);//构造string流
        string word,ans;
        int cnt=0;
        while(in>>word)//不断获取单词,存入vec中
        {
            vec.push_back(word);
            cnt+=word.size();//记录单词总长度
        }
        if(vec.size()==1)//如果只有一个单词,单独处理
            ans=vec[0];
        else
        {
            string space((text.size()-cnt)/(vec.size()-1),' ');//计算并生成单词间的空格
            for(int i=0;i<vec.size()-1;++i)//重新生成字符串,最后一个单独处理
                ans+=vec[i]+space;
            ans+=vec.back();
        }
        return ans+string(text.size()-ans.size(),' ');//用空格补齐并返回
    }
};

参考链接:https://leetcode.cn/problems/rearrange-spaces-between-words/solution/c-stringstream-by-heren1229-tg6v/

解题思路三:暂无