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

Could you do it in-place without allocating extra space?

1、刚开始的想法是找到第一个和最后一个单词,然后交换他们两个,但是遇到的问题是两个单词长度不一致的时候会很麻烦,就需要将剩余的字符串整个进行移动,麻烦,且效率很低。

2、参考了discuss,发现很其实想法很简单,就是分两步:第一步就是把整个字符串倒过来,第二步就是再翻回来之后再把每个单词反转一次就好了。

public class Solution {
    public void reverseWords(char[] s) {
        int len = s.length;
        reverse(s, 0, len - 1);
        int start = 0;
        for (int i = 0; i < len - 1; i++){
            if (s[i] == ‘ ‘){
                reverse(s, start, i - 1);
                start = i + 1;
            }
        }
        reverse(s, start, len - 1);
    }
    private void reverse(char[] s, int start, int end){
        while (start < end){
            char ch = s[start];
            s[start] = s[end];
            s[end] = ch;
            start++;
            end--;
        }
    }
}
时间: 2024-11-07 02:05:31

leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java的相关文章

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

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

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

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

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

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