Reverse Words in a String | LeetCode OJ | C++

我的思路:先读取每一个单词,存放到容器中;读取完毕后,将容器中的单词倒序写入输出中。

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void f(string &s){
    vector<string> vs;
    string temp="";
    int i=0;
    while(s[i]!=‘\0‘){               //遍历直到结尾
        if (s[i]==‘ ‘){              //遇到空格
            if (!temp.empty())
                vs.push_back(temp); //单词放入容器中
            ++i;                    // 跳过空格
            temp="";        }
        else{
            temp.push_back(s[i]);   //将字母加到temp字符串后面
            ++i;
        }
    }
    if (!temp.empty())
        vs.push_back(temp);       //将最后一个单词放入容器中

    int len=vs.size();            //倒序遍历容器
    string res="";
    for(int j=0;j<len;++j){
        res.append(vs[len-j-1]);
        if(j!=len-1)
            res.push_back(‘ ‘);
    }
    s=res;
}

int main(){
    string s;
    getline(cin,s);
    f(s);
    cout<<s<<endl;
}
时间: 2024-07-28 23:07:19

Reverse Words in a String | LeetCode OJ | C++的相关文章

Reverse Words in a String leetcode java

题目: 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 con

Reverse Vowels of a String Leetcode

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 include

(Easy) Reverse Vowels of a String - LeetCode

Description: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" Note:The vowels does not

Reverse Words in a String——LeetCode

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. Clari

Reverse Words in a String leetcode

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a word?A sequence of n

151. Reverse Words in a String Leetcode Python

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. linear time: 1. go through the

[LeetCode] Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

LeetCode OJ - 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". 解题思路: 1.先对字符串进行一次总的翻转 2.以空格为单位,划分单词,然后对每个单词再进行一次翻转 代码: class Solution { public: void swap(char& a, char

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2&q