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:

  1. 采用一个vector,来存放中间结果
  2. 将vector的结果倒序放入原字符串中。

思路2:

  1. 在字符串分割的时候,直接将单词倒序相加到临时串。
  2. 将临时串的结果放入原串中。

代码如下:(采用思路2)

class Solution {
public:
    void reverseWords(string &s) {
        string result = "";
        const int sLen = s.length();
        char *cs = new char[sLen + 1];
        strcpy(cs, s.data());
        char *p;
        p = strtok(cs, " ");
        while (p)
        {
            string tmp(p);
            result = tmp + " " + result;
            p = strtok(NULL, " ");
        }
        s.clear();
        s = result.substr(0,result.size() - 1);//将最开始加入的" "删除掉
    }
};

2016-08-18 20:32:00

时间: 2024-10-10 09:21:58

leetCode 151. Reverse Words in a String 字符串反转 | Medium的相关文章

[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]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 --------- 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 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] 从左往右遍历一次,每当遇到空格暂停

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

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】Reverse Words in a String

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

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