LeetCode: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".

  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.
  • 题目大意:给定一个字符串,对字符串进行逆转。
  • 解题思路:看到这道题目有两种思路:

    1)用两个指针从前到后扫描,分开单词,先对每个单词进行逆转,最后再对整个字符串逆转;

    比如题目中给的例子:先对每个单词进行逆转得到的结果:"eht yks si eulb",然后再整体逆转即可得到"blue is sky the"。

    2)根据空格切分字符串,将切分得到的单词存到vector中,然后将vector中的单词从末尾开始输出即可。

    在衡量了两种方法之后,觉得第二种方法代码更加简洁方便,便选择了第二种思路。   

  • 实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void  reverseWords(string &s) {
         int  i=0,j=0;
         int  len = s.length();
         vector<string> splitResult;
             
         while (i<len)
         {
             if (s[i]== ' ' )
                 i++;
             else
             {
                 j=i+1;
                 while (j<=len)
                 {
                     if (s[j]== ' ' ||j==len)
                     {
                         string tempStr = s.substr(i,j-i);
                         splitResult.push_back(tempStr);
                         i=j+1;
                         break ;
                     }
                     else
                         j++;
                 }
                        
             }  
         }
         int  size = splitResult.size();
         if (size>0)
         {
             s= "" ;
             for (i=size-1;i>0;i--)
                 s+=splitResult[i]+ " " ;
             s+=splitResult[i];
         }
         else
         {
             s= "" ;
         }
         
     }

  注意的地方:我一开始提交就提示错误,要考虑到空字符串以及只有空格组成的字符串,因此要在最后作一个判断,如果splitResult为空,则直接把s赋值为""即可。  

  本文转载自海 子博客园博客,原文链接:http://www.cnblogs.com/dolphin0520/p/3700019.html如需转载自行联系原作者


相关文章
|
4月前
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
30 0
|
7月前
CF1553B Reverse String(数学思维)
CF1553B Reverse String(数学思维)
23 0
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
3月前
|
Go 机器学习/深度学习 Rust
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
35 0
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
|
5月前
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
19 0
|
7月前
|
机器学习/深度学习
CF71A Way Too Long Words(string简单模拟)
CF71A Way Too Long Words(string简单模拟)
36 0
|
存储 canal 算法
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List