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 = "the sky is blue",
return "blue is sky the".

Could you do it in-place without allocating extra space? 也就是不能用I 那样存个暂存word string来翻转

分析抄达达“http://www.danielbit.com/blog/puzzle/leetcode/leetcode-reverse-words-in-a-string-ii

[分析]
该题在LeetCode中假设开头和结尾没有空格,而且单词之间只有一个空格。但其实不需要这些假设也是可以的,就是代码会比较复杂。
思路就是两步走,第一步就是将整个字符串翻转。然后从头逐步扫描,将每个遇到单词再翻转过来。

[注意事项]
1)如果是Java,应该跟面试官指出String是immutable,所以需要用char array来做。

2)follow-up问题:k-step reverse。也就是在第二部翻转的时候,把k个单词看作一个长单词,进行翻转。

code 我觉得这个写的清楚 ref

http://www.cnblogs.com/EdwardLiu/p/4306561.html

public class Solution {
    public void reverseWords(char[] s) {
        if (s.length == 0) return;
        reverse(s, 0, s.length-1);
        int last = 0;
        for (int i=0; i<s.length; i++) {
            if (s[i] == ‘ ‘) {
                reverse(s, last, i-1);
                last = i + 1;
            }
        }
    }

    public void reverse(char[] s, int l, int r) {
        while (l <= r) {
            int temp = s[l];
            s[l] = s[r];
            s[r] = temp;
            l++;
            r--;
        }
    }
}
时间: 2024-10-19 04:58:09

Reverse Words in a String II的相关文章

[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

[Swift]LeetCode186. Reverse Words in a String II $ 翻转字符串中的单词 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

186. Reverse Words in a String II

https://leetcode.com/problems/reverse-words-in-a-string-ii/#/description 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

leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

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 =

leetcode 186: Reverse Words in a String II

Total Accepted: 421 Total Submissions: 1404 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 separat

186. Reverse Words in a String II 翻转有空格的单词串 里面不变

[抄题]: Given an input string , reverse the string word by word. Example: Input: ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l

[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". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

[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] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns