Reverse Words in a String

原创 2015年11月17日 21:53:36
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<string>
#include<vector>
using namespace std;

class Solution {
public:
string reverseWords(string str) {
if (str == "")
return str;
else {
int i = 0;
int j = str.size() - 1;
vector<string> re;


while (i<str.size()&& str[i] == ' ') i++;
while (j>0 && str[j] == ' ') j--;


if (i >= j)
return "";
else{
int idx;
int wdx = j + 1;
string word;
for (idx = j; idx >= i; idx--){
if (str[idx] == ' ' && str[idx - 1] != ' ') {


word = str.substr(idx + 1, wdx - idx - 1);
re.push_back(word);
wdx = idx;
}
else if (idx == i){
word = str.substr(i, wdx - i);
re.push_back(word);
}
}


int t;
string str1;
for (t = 0; t < re.size(); t++){
str1 = str1 + " " + re[t];
}


return str1.substr(1, str1.size() - 1);
}
}
}




};




int main(){


Solution exp;
string str =" ";
string re = exp.reverseWords(str);
cout << re;
system("pause");
return 1;






}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

反向排列句子中的词(Reverse Words in a String)

今天开始在点击打开链接

Reverse Words in a String

Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, r...

将一个英文句子单词顺序逆置(LeetCode | Reverse Words in a String)

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky...

[LeetCode186]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...

[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", ret...

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”, ...

《leetCode》:Reverse Words in a String

题目Given an input string, reverse the string word by word.For example, Given s = "the sky is blue", r...

LeetCode之186_Reverse Words in a String 字符串词序反转算法

给定一个字符串S,反转其中存在的单词顺序,将

[leetcode]151. Reverse Words in a String@Java解题报告

https://leetcode.com/problems/reverse-words-in-a-string/tabs/description Given an input s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)