• 【子串】151. 反转字符串中的单词【中等】


    反转字符串中的单词

    • 给你一个字符串 s ,请你反转字符串中 单词 的顺序。

    单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。

    返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。

    注意输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。

    解题思路

    • 要反转字符串中的单词顺序,可以先将整个字符串进行反转,然后再逐个单词进行反转。
    • 这样就可以得到单词顺序颠倒的结果字符串。

    java实现

    public class ReverseWords {
        public String reverseWords(String s) {
            // 去除字符串首尾空格,并将连续多个空格替换为一个空格
            //"\s" 表示空白字符,包括空格、制表符、换行符等
            //"\s+" 表示匹配一个或多个连续的空白字符
            s = s.trim().replaceAll("\\s+", " ");
            // 将字符串转换为字符数组
            char[] chars = s.toCharArray();
            // 反转整个字符串
            reverse(chars, 0, chars.length - 1);
            
            // 反转每个单词
            int start = 0;
            for (int end = 0; end < chars.length; end++) {
                if (chars[end] == ' ') {
                    reverse(chars, start, end - 1);
                    start = end + 1;
                }
            }
            // 反转最后一个单词
            reverse(chars, start, chars.length - 1);
            
            return new String(chars);
        }
        
        // 辅助函数:反转字符数组中指定范围的字符
        private void reverse(char[] chars, int left, int right) {
            while (left < right) {
                char temp = chars[left];
                chars[left] = chars[right];
                chars[right] = temp;
                left++;
                right--;
            }
        }
    
        public static void main(String[] args) {
            ReverseWords reverseWords = new ReverseWords();
            String s1 = "the sky is blue";
            System.out.println("Test Case 1:");
            System.out.println("Input: \"the sky is blue\"");
            System.out.println("Reversed Words: \"" + reverseWords.reverseWords(s1) + "\""); // Expected: "blue is sky the"
    
            String s2 = "  hello world  ";
            System.out.println("\nTest Case 2:");
            System.out.println("Input: \"  hello world  \"");
            System.out.println("Reversed Words: \"" + reverseWords.reverseWords(s2) + "\""); // Expected: "world hello"
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    时间空间复杂度

    • 时间复杂度: 遍历字符串的时间复杂度为 O(n),其中 n 是字符串的长度。
    • 空间复杂度: 使用了额外的字符数组,空间复杂度为 O(n),其中 n 是字符串的长度。
  • 相关阅读:
    http在安卓9.0以上版本无法获取数据问题(备忘)
    Java可变参数和不可变集合
    鉴源论坛 · 观模丨嵌入式实时操作系统的形式化验证
    CesiumJS PrimitiveAPI 高级着色入门 - 从参数化几何与 Fabric 材质到着色器 - 下篇
    戴尔PowerEdge服务器R450 RAID配置步骤
    Python如何在日志中隐藏明文密码
    macOS查端口占用进程
    高效使用Python的subprocess模块:基本用法与进阶技巧
    冰冰学习笔记:初识环境变量
    分布式中灰度方案实践
  • 原文地址:https://blog.csdn.net/FLGBgo/article/details/138792946