其他分享
首页 > 其他分享> > LeetCode 0151 Reverse Words in a String

LeetCode 0151 Reverse Words in a String

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
使用字符串API,先按空格切开,得到所有的单词序列,然后反转序列,最后返回按空格拼接的反转序列。

2、代码实现

package Q0199.Q0151ReverseWordsinaString;

import java.util.Arrays;
import java.util.Collections;

public class Solution {
    public String reverseWords(String s) {
        String[] words = s.trim().split("\\s+");
        Collections.reverse(Arrays.asList(words));
        return String.join(" ", words);
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)

3. Solution 2

1、思路分析
Step 1: reverse the whole char array
Step 2: reverse each word writing at index idx
Step 3: erase useless characters at the end of string

2、代码实现

package Q0199.Q0151ReverseWordsinaString;

public class Solution2 {
    public String reverseWords(String s) {
        if (s == null) return null;

        char[] chars = s.toCharArray();
        int n = chars.length;

        reverse(chars, 0, n - 1);
        reverseWords(chars, n);
        return cleanSpaces(chars, n);
    }

    private String cleanSpaces(char[] chars, int n) {
        int start = 0, end = 0;
        while (end < n) {
            while (end < n && chars[end] == ' ') end++;
            while (end < n && chars[end] != ' ') chars[start++] = chars[end++];
            while (end < n && chars[end] == ' ') end++;
            if (end < n) chars[start++] = ' ';
        }
        return new String(chars).substring(0, start);
    }

    private void reverseWords(char[] chars, int n) {
        int start = 0, end = 0;
        while (start < n) {
            while (start < end || start < n && chars[start] == ' ') start++;
            while (end < start || end < n && chars[end] != ' ') end++;
            reverse(chars, start, end - 1);
        }
    }

    private void reverse(char[] chars, int start, int end) {
        while (start < end) {
            char tmp = chars[start];
            chars[start++] = chars[end];
            chars[end--] = tmp;
        }
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)

4. Solution 3

1、思路分析
双端队列。由于双端队列支持从队列头部插入的方法,因此可以沿着字符串一个个单词处理,然后将单词压入队列的头部,再将队列转换成字符串即可。

2、代码实现

package Q0199.Q0151ReverseWordsinaString;

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution3 {
    public String reverseWords(String s) {
        int start = 0, end = s.length() - 1;
        // 去掉字符串开头的空白字符
        while (start <= end && s.charAt(start) == ' ') ++start;
        // 去掉字符串末尾的空白字符
        while (start <= end && s.charAt(end) == ' ') --end;

        Deque<String> deque = new ArrayDeque<>();
        StringBuilder word = new StringBuilder();

        while (start <= end) {
            char c = s.charAt(start);
            if (word.length() != 0 && c == ' ') {
                deque.offerFirst(word.toString());
                word.setLength(0);
            } else if (c != ' ') {
                word.append(c);
            }
            ++start;
        }
        deque.offerFirst(word.toString());

        return String.join(" ", deque);
    }
}

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)

标签:end,String,chars,start,while,复杂度,LeetCode,Reverse
来源: https://www.cnblogs.com/junstat/p/16304125.html