186. Reverse Words in a String II

本文详细解析了如何使用C++实现字符串单词级别的反转,包括去除多余空格、保持单词间单个空格,以及避免首尾空格的方法。通过具体示例和代码片段,展示了多种反转字符串的算法实现,特别关注了在不同情况下如何正确处理空格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

186. Reverse Words in a String II

Given an input string, reverse the string word by word.

Example 1:

Input: "the sky is blue"
Output: "blue is sky the"

Example 2:

Input: "  hello world!  "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Note:

A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.

方法1:

思路:

和151. Reverse Words in a String 一样。但是由于不考虑空格可以直接按照先大翻转再小翻转。

参考:grandyang https://www.cnblogs.com/grandyang/p/5186294.html
这里的loop写法非常值得借鉴

易错点

  1. loop很容易写坏
  2. 下面的写法就谜一样的错了
//["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
//["e","u","l","b"," ","s","i"," ","y","k","s"," ","e","h","t"]
//  s               e

//["b","l","u","e"," ","s","i"," ","y","k","s"," ","e","h","t"]
//  s               e
//                      s       e

//["b","l","u","e"," ","i","s"," ","y","k","s"," ","e","h","t"]
//                      s       e
//                                  s           e

//["b","l","u","e"," ","i","s"," ","s","k","y"," ","e","h","t"]
//                                  s           e
//                                              s             e
class Solution {
public:
    void reverseWords(vector<char>& str) {
        reverse(str.begin(), str.end());
       
        for (int i = 0, j = 0; i < str.size(); i = j + 1){
            for (j = i; j < str.size(); j ++){
                if (str[j] == ' ') break;
            }
            reverse(str.begin() + i, str.begin() + j);
        }
        return;
    }
};

class Solution {
public:
    void reverseWords(vector<char>& str) {
        reverse(str.begin(), str.end());
        int start = 0, end = 0;
        int n = str.size();
        while (end < n && start < n){
            while (str[end] != ' '){
                end++;
            }
            //reverse(str.begin() + start, str.begin() + end);
    
            start = end + 1;
            end = start;
        }
        return;
    }
};
class Solution {
public:
    void reverseWords(vector<char>& str) {
        reverse(str, 0, str.size() - 1);
        int begin = -1, end = 0, n = str.size();
        for (int end = 0; end < n; end++) {
            if (begin == -1) {
                if (str[end] != ' ') begin = end;
            }
            // 这里不能是else,因为可能begin == end
            if (end == n - 1 || str[end + 1] == ' ') {
                reverse(str, begin, end);
                begin = -1;
            }
        }
        return;
    }
    void reverse(vector<char>& str, int left, int right) {
        while (left < right) {
            swap(str[left++], str[right--]);
        }
        return;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值