leetcode6 Reverse Words in a String 单词取反

  Reverse Words in a String  单词取反

    [email protected]

Question:

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

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

void reverseWords(string &s) {

vector <string>des;

if(s.empty())

return;

int len = s.size();

for(int i = 0; i < len; i++){

if(s[i] == ‘ ‘)

continue;

string word;

while(i < len && s[i] != ‘ ‘){

word += s[i];

i++;

}

des.push_back(word);

}

reverse(des.begin(), des.end());

if(des.empty())

s = "";

else {

s.clear();

int j ;

for(j = 0; j < des.size() -1; j++){

s += des[j];

s += ‘ ‘;

}

s += des[j];

}

}

时间: 2024-10-16 05:02:41

leetcode6 Reverse Words in a String 单词取反的相关文章

Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode

LeetCode新题,但是比较简单,直接用栈即可 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-sp

leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)

题目如下: 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 c

[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. Clarification: What constitutes a

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 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

Reverse Words in a String

Problem: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 这个题目叙述的不够精确,有些边界的情况需要考虑.题目的下方给出了Clarification澄清了几个问题,也是面试的时候需要确认的. 第一个问题是什么构成了一个词(word),回答是一串不包含空格(non

151. Reverse Words in a String (String)

思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最后一个单词要做特殊处理. class Solution { public: void reverseWords(string &s) { int start = 0; int end=-1; int i = 0; int cur = 0; // point to the string with ex

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