Reverse Words in a String

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

For example,
Given s = "the sky is blue",
return "blue is sky the".

click to show clarification.

Clarification:

 

  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

提交了好多遍,终于成功了,使用了sstream来分割字符串,使用栈来存放分割之后的字符串,然后出栈就是逆序了。。

 

C++实现代码:

#include<iostream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;

class Solution
{
public:
    void reverseWords(string &s)
    {
        if(s.empty())
            return;
        stringstream ss(s);
        s.clear();
        stack<string> st;
        string tmp;
        while(ss>>tmp)
        {
            st.push(tmp);
        }
        while(!st.empty())
        {
            tmp=st.top();
            st.pop();
            if(!st.empty())
                s+=tmp+' ';
            else
                s+=tmp;
        }
    }
};

int main()
{
    Solution s;
    string ss="  the   sky   is    blue  ";
    cout<<ss.length()<<endl;
    cout<<ss<<endl;
    s.reverseWords(ss);
    cout<<ss.length()<<endl;
    cout<<ss<<endl;
}

运行结果:

运行结果:

 

时间: 2024-10-25 06:18:08

Reverse Words in a String的相关文章

[LeetCode]151.Reverse Words in a String

题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification.

[LeetCode 第3题] -- Reverse Words in a String

题目链接: Reverse Words in s String 题目意思: 给定一个字符串,要求把字符串中的"单词"反转                 1. 所谓"单词"指的的连续的非空白字符                 2. 必须把前后缀空格去掉                 3. 连续多个空格要合并为一个 代码 class Solution { public: void reverseWords(string &s); }; void Solut

[LeetCode] Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". 解题思路 双指针,一个往前移动,一个往后移动,找到元音字母时

[LeetCode] Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: 1.What constitutes a word? A sequence of non-space characters constitutes a word. 2.Could the in

[LeetCode]--345. Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". Note: The vowels does not incl

LeetCode 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

1102: 数字反转

1102: 数字反转 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 27  Solved: 6 [Submit][Status][Web Board] Description 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(如:输入-380,输出-83). Input 输入共1行,一个整数N. Output 输出共1行,一个整数,表示反转后的新数. Sa

Java基础13-总结StringBuffer,StringBuilder,数组高级,Arrays,Integer,Character

你需要的是什么,直接评论留言. 获取更多资源加微信公众号"Java帮帮" (是公众号,不是微信好友哦) 还有"Java帮帮"今日头条号,技术文章与新闻,每日更新,欢迎阅读 学习交流请加Java帮帮交流QQ群553841695 分享是一种美德,分享更快乐! 1:StringBuffer(掌握) (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了    一个字符串缓冲区类.StringBuffer供我们使用. (

c#-C#程序问题,,C#输入123456然后逆序输出,程序怎么写?

问题描述 C#程序问题,,C#输入123456然后逆序输出,程序怎么写? C#输入123456然后逆序输出,程序怎么写啊,求大神帮帮忙,自己刚学,实在写不来 解决方案 string s=""123456"";for(int i=s.Length-1;i>=0;i--)Console.Write(s[i]); 解决方案二: foreach (var p in numbers.Reverse()) { Console.WriteLine(p); } 解决方案三: