Reverse Words in a String--not finished yet

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.

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.

Analyse: For my first thought, I will reverse the string first, then every word in the string has been reversed. Then I find each word, and reverse them. Space complexity is O(1). Time complexity is O(1).

 1 class Solution {
 2 public:
 3     void reverse(string& s, int start, int end){
 4     if(start >= end) return;
 5     for(int i = start, j = end; i < j; i++, j--)
 6         swap(s[i], s[j]);
 7 }
 8
 9
10 void reverseWords(string& s) {
11     if(s.size() < 2) return;
12
13     //first inverse the string
14     //every word has been reversed
15     reverse(s, 0, s.size() - 1);
16
17     //for each word, reverse the word back
18     int start = 0;
19     for(int i = 0; i < s.size(); i++){
20         while(s[i] != ‘ ‘) i++;
21         if(i >= s.size()) i = s.size();
22         reverse(s, start, i - 1);
23         start = i + 1;
24     }
25
26     for(int i = 0; i < s.size(); i++)
27         cout<<s[i];
28
29 }
30 };
时间: 2024-08-08 13:46:54

Reverse Words in a String--not finished yet的相关文章

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

Reverse Words in a String (JAVA)

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 1 public class Solution { 2 public String reverseWords(String s) { 3 if(s.equals(""))return s; 4 String

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

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",return "blue is sky the". click to show clarification. Clarification: What constitutes a word?A sequence of non-s

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

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

leetCode 151. Reverse Words in a String 字符串反转 | Medium

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". 题目大意: 输入一个字符串,将单词序列反转. 思路1: 采用一个vector,来存放中间结果 将vector的结果倒序放入原字符串中. 思路2: 在字符串分割的时候

reverse words in a string(java 注意一些细节处理)

题目: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". 解析:将字符串中的单词逆序输出 借助一个堆栈,从前向后遍历字符串,遇到空,跳过,直到非空字符,拼接word,等再次遇到空时,得到一个word,加入堆栈, 以此类推,直到