151. Reverse Words in a String

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

Example 1:

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

Example 2:

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

Example 3:

Input: s = "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.

Constraints:

  • 1 <= s.length <= 104
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '.
  • There is at least one word in s.

Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

class Solution {
public:
    void reverse(string &s,int start,int end){
        for(int i=start,j=end-1;i<j;i++,j--){
            swap(s[i],s[j]);
        }
    }
    void removeExtraSpace(string &s){
        int slow=0;
        for(int fast=0;fast<s.size();fast++){
            if(s[fast]!=' '){
                if(slow!=0)s[slow++]=' ';
                while(fast<s.size() && s[fast]!=' ')s[slow++]=s[fast++];
            }
        }
        s.resize(slow);
    }
    string reverseWords(string s) {
        removeExtraSpace(s);
        reverse(s,0,s.size());
        int start=0;
        for(int i=0;i<=s.size();i++){
            if(i==s.size() || s[i]==' '){
                reverse(s,start,i);
                start=i+1;
            }
        }
        return s;
    }
};

注意:

        1.需要删除多余空格,但是每个单词之间又需要一个空格

        2.总体思路:先把多余空格删除,再把整个字符串反转,再依次把每一个单词反转

        3.reverse函数很简单,不过多赘述

        4.removeExtraSpace函数:使用快慢指针,重点是当fast所在位置不为空格且slow>0就需要手动添加一个空格。

相关推荐

  1. LeetCode--151

    2024-07-13 21:06:01       45 阅读
  2. Leetcode 11-15

    2024-07-13 21:06:01       52 阅读
  3. 面试经典150题(101-104)

    2024-07-13 21:06:01       42 阅读
  4. 面试经典150题(111-113)

    2024-07-13 21:06:01       36 阅读
  5. IOS面试题object-c 111-115

    2024-07-13 21:06:01       33 阅读
  6. 151 shell编程

    2024-07-13 21:06:01       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 21:06:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 21:06:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 21:06:01       58 阅读
  4. Python语言-面向对象

    2024-07-13 21:06:01       69 阅读

热门阅读

  1. 力扣--20. 有效的括号

    2024-07-13 21:06:01       18 阅读
  2. RC-u3 跑团机器人

    2024-07-13 21:06:01       16 阅读
  3. 设计模式的七项原则

    2024-07-13 21:06:01       21 阅读
  4. 力扣2381.字母移位II

    2024-07-13 21:06:01       20 阅读
  5. Transformer模型:WordEmbedding实现

    2024-07-13 21:06:01       16 阅读
  6. stm32高级定时器

    2024-07-13 21:06:01       16 阅读
  7. Jupyter Notebook 使用教程

    2024-07-13 21:06:01       20 阅读
  8. 基于SpringBoot就医管理系统设计与实现(测试项)

    2024-07-13 21:06:01       20 阅读
  9. C语言 求两个整数的最大公约数和最小公倍数

    2024-07-13 21:06:01       17 阅读
  10. 《我的编程学习之旅启程》

    2024-07-13 21:06:01       20 阅读