Leetcode 151题 Reverse Words in a String

时间:2014.05.10

地点:基地

心情:准备刷下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".

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.将字符串从后往前遍历,每次遍历前都为了剔除空格,从当前位置往前找,直到不是空格为止,停止变化索引。

2.继续将索引向前移动,直到遇到空格为止,这过程中遍历的字符都是属于一个单词中的。

3.重复上面两个过程,直到字符串的字符全部遍历完。

------------------------------------------------------------------------------------------

三、技巧

1.由于我们从后往前遍历,每捕获到一个完整的单词,我们均将它在反转得到单词的正序追加到结果中

2.单词与单词之间还需要有一个空格,于是我们在存储捕获单词的临时字符串中首先初始化开始第一个字符为空格,反转后它将被变换到单词尾。

-----------------------------------------------------------------------------------------

四、AC源码

class Solution {
public:
    void reverseWords(string &s) {
        string result_str = "",temp_str = "";
		for (int i = s.length()-1; i >= 0; --i)
		{
			while ((i >= 0) && (‘ ‘ == s[i]))
				--i;
			temp_str = "";
			if (i >= 0)
				temp_str.push_back(‘ ‘);
			while ((i>=0)&&(‘ ‘!=s[i]))
				temp_str.push_back(s[i--]);
			reverse(temp_str.begin(), temp_str.end());
			result_str.append(temp_str);
		}
		s = result_str;
		if (!s.empty())
			s.pop_back();
	}
};

-----------------------------------------------------------------------------------------

五、几个需要注意的地方

1.输出结果最后不能有空格

2.边界处理,比如输入长度为0的字符串或者全是空格的字符串

Leetcode 151题 Reverse Words in a String,布布扣,bubuko.com

时间: 2025-01-02 03:03:48

Leetcode 151题 Reverse Words in a String的相关文章

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

LeetCode算法题-Reverse Bits(Java实现)

这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:43261596 输出:964176192 说明:43261596以二进制表示为00000010100101000001111010011100, 964176192以二进制表示为00111001011110000010100101000000. 本次解题使用的开发工具是eclipse,jdk使用的版本是1

[LeetCode][JavaScript][Python]Reverse Vowels of a String

Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 将字符串中

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 应该算不上有难度. class Solution { p

Leetcode题目:Reverse Vowels of a String

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". 题目解答: 要求将字符串中所有的元音字母逆转,辅音字

<LeetCode OJ> 345. Reverse Vowels of a String

Total Accepted: 537 Total Submissions: 1488 Difficulty: Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode"

Leetcode解题笔记-Reverse Words in a String

题目要求: 给定一个字符串由不同单词组成,返回其相反顺序,中间可能有多余字符: 例如: Given s = "the sky is blue",return "blue is sky the". 个人解法: 1.暴力,主要是对于两个单词中间存在多个空格的处理. 2. 利用栈来存储临时变量. 3.缺点,空间利用太大 代码: public String reverseWords(String s) { Stack<String> stack = new St

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