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

[思路]

反转整个字符串, 再反转每个单词即可

[注意]

None

[DODE]

public class Solution {
    //the sky is blue
    public void reverseWords(char[] s) {
        //input check
        if(s.length<=1) return;

        swap(s, 0, s.length-1);

        int i=0, j=1;

        while(j<s.length) {
            while(j<s.length && s[j]!=' ') {
                ++j;
            }
            swap(s, i, j-1);
            i = j+1;
            j = i+1;
        }

    }

    private void swap(char[] s, int i, int j) {
        while(i<j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            ++i;
            --j;
        }
    }
}
时间: 2024-10-29 19:07:16

leetcode 186: Reverse Words in a String II的相关文章

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 =

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