Leetcode 151. 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".

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.



[Idea]

从左往右遍历一次,每当遇到空格暂停,记录暂停位置作为开始点,移动二指针到下一个空格前的位置或者字符串末尾,则找到一个word。用一个新的空字符串result倒序存储每一个word。

双指针的方法。思想和实现都很简单。

[Code]

 1 class Solution {
 2 public:
 3     void reverseWords(string &s) {
 4         string result = "";
 5         for (int i = 0; i < s.length(); i++) {
 6             if (s[i] != ‘ ‘) {
 7                 int pos = i;
 8                 while (i < s.length() && s[i] != ‘ ‘) i++;
 9                 if (result.length() > 0) result = ‘ ‘ + result;
10                 result = s.substr(pos, i - pos) + result;
11                 i--;
12             }
13         }
14         s = result;
15     }
16 };

update:

 1 class Solution {
 2 public:
 3     void reverseWords(string &s) {
 4         string result;
 5         int i = 0;
 6         while(i < s.size()) {
 7             if(i < s.size() && s[i] == ‘ ‘) {i++; continue;}
 8             string word;
 9             while(i < s.size() && s[i] != ‘ ‘) {
10                 word += s[i];
11                 i++;
12             }
13             if(result != "") result = ‘ ‘ + result;
14             result = word + result;
15         }
16         s = result;
17     }
18 };

[Reference]

喜刷刷

时间: 2024-08-04 12:36:52

Leetcode 151. Reverse Words in a String 解题报告的相关文章

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

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: 在字符串分割的时候

[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

Java for 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". 解题思路: 本题方法多多,最简单的方式直接按“ ” spilt即可,JAVA实现如下: public String reverseWords(String s) { if (s == null || s.length()

leetcode 151. 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". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. 倒序字符串. 注意使用BufferString,因为如果使用Stri

LeetCode 151 reverse word 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. 这题遇到的困难主要是细节地方处理的不好:(1)空格的处理,(2

LeetCode OJ1: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". 解题思路: 先利用split()方法将句子按空格分为几个单词的列表, 然后reverse()函数使列表逆序, 最后join函数以空格为间隔拼成字符串返回结果. Python代码: class Solution:  

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

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