LeetCode151——翻转字符串里的单词

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/86248468

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/description/

题目描述:

知识点:正则表达式

思路:利用正则表达式分割原字符串,再倒序输出即可

时间复杂度和空间复杂度均是O(n),其中n为原字符串中的单词个数。

JAVA代码:

public class Solution {
    public String reverseWords(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] strings = s.split("\\s+");
        for(int i = strings.length - 1; i >= 0; i--){
            stringBuilder.append(strings[i]).append(" ");
        }
        return stringBuilder.toString().trim();
    }
}

LeetCode解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/86248468
今日推荐