反转字符串中的单词 (Reverse Words in a String)

 

思路:官方题解

// @Title: 反转字符串中的单词 (Reverse Words in a String)
// @Author: qisiii
// @Date: 2024-09-13 00:19:56
// @Runtime: 7 ms
// @Memory: 41.4 MB
// @comment: 官方题解
// @flag: WHITE
class Solution {
    public String reverseWords(String s) {
        StringBuilder sb = trimSpaces(s);

        // 翻转字符串
        reverse(sb, 0, sb.length() - 1);

        // 翻转每个单词
        reverseEachWord(sb);

        return sb.toString();
    }

    public StringBuilder trimSpaces(String s) {
        int left = 0, right = s.length() - 1;
        // 去掉字符串开头的空白字符
        while (left <= right && s.charAt(left) == ' ') {
            ++left;
        }

        // 去掉字符串末尾的空白字符
        while (left <= right && s.charAt(right) == ' ') {
            --right;
        }

        // 将字符串间多余的空白字符去除
        StringBuilder sb = new StringBuilder();
        while (left <= right) {
            char c = s.charAt(left);

            if (c != ' ') {
                sb.append(c);
            } else if (sb.charAt(sb.length() - 1) != ' ') {
                sb.append(c);
            }

            ++left;
        }
        return sb;
    }

    public void reverse(StringBuilder sb, int left, int right) {
        while (left < right) {
            char tmp = sb.charAt(left);
            sb.setCharAt(left++, sb.charAt(right));
            sb.setCharAt(right--, tmp);
        }
    }

    public void reverseEachWord(StringBuilder sb) {
        int n = sb.length();
        int start = 0, end = 0;

        while (start < n) {
            // 循环至单词的末尾
            while (end < n && sb.charAt(end) != ' ') {
                ++end;
            }
            // 翻转单词
            reverse(sb, start, end - 1);
            // 更新start,去找下一个单词
            start = end + 1;
            ++end;
        }
    }
}

+++ title = “反转字符串中的单词 (Reverse Words in a String)” draft = false +++

思路:

// @Title: 反转字符串中的单词 (Reverse Words in a String)
// @Author: qisiii
// @Date: 2024-09-13 00:19:04
// @Runtime: 35 ms
// @Memory: 44 MB
// @comment: 
// @flag: 
class Solution {
    public String reverseWords(String s) {
        s = deleteBlank(s);
        String all = reverseStr(s, 0, s.length() - 1);
        return reverseEachWord(all);
    }

    private static String deleteBlank(String s) {
        //删除单词间多余的空格
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<s.length(); i++) {
            if (i+1<s.length()&&s.charAt(i) == ' '&&s.charAt(i+1)==' ') {
                continue;
            }else{
                sb.append(s.charAt(i));
            }
        }
        return sb.toString().trim();
    }

    private String reverseStr(String s, int left, int right) {
        char[] arr = s.toCharArray();
        int start = left, end = right + 1;
        while (left < right) {
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
        return new String(Arrays.copyOfRange(arr, start, end));
    }

    private String reverseEachWord(String s) {
        char[] arr = s.toCharArray();
        StringBuilder str = new StringBuilder();
        int start = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == ' ') {
                str.append(reverseStr(s, start, i - 1));
                str.append(' ');
                start = i + 1;
            }
        }
        str.append(reverseStr(s, start, arr.length - 1));
        return str.toString();
    }

}

思路:使用了split

// @Title: 反转字符串中的单词 (Reverse Words in a String)
// @Author: qisiii
// @Date: 2024-09-12 20:58:03
// @Runtime: 6 ms
// @Memory: 43.4 MB
// @comment: 使用了split
// @flag: WHITE
class Solution {
    public String reverseWords(String s) {
        s=s.trim();
        String[] strs=s.split(" ");
        StringBuilder build=new StringBuilder();
        for(int i=strs.length-1;i>=0;i--){
            if(strs[i].length()>0){
            build.append(strs[i]+" ");
            }
        }
        return build.toString().trim();
    }
}

+++ title = “反转字符串中的单词 (Reverse Words in a String)” draft = false +++

思路:

// @Title: 反转字符串中的单词 (Reverse Words in a String)
// @Author: qisiii
// @Date: 2024-09-13 00:16:58
// @Runtime: 32 ms
// @Memory: 44.2 MB
// @comment: 
// @flag: 
class Solution {
    public String reverseWords(String s) {
        s = deleteBlank(s);
        String all = reverseStr(s, 0, s.length() - 1);
        return reverseEachWord(all);
    }

    private static String deleteBlank(String s) {
        char[] chars = s.toCharArray();
        // 删除单词间多余的空格
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (i + 1 < chars.length && chars[i] == ' ' && chars[i + 1] == ' ') {

            } else {
                sb.append(chars[i]);
            }
        }
        return sb.toString().trim();
    }

    private String reverseStr(String s, int left, int right) {
        char[] arr = s.toCharArray();
        int start = left, end = right + 1;
        while (left < right) {
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }
        return new String(Arrays.copyOfRange(arr, start, end));
    }

    private String reverseEachWord(String s) {
        char[] arr = s.toCharArray();
        StringBuilder str = new StringBuilder();
        int start = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == ' ') {
                str.append(reverseStr(s, start, i - 1));
                str.append(' ');
                start = i + 1;
            }
        }
        str.append(reverseStr(s, start, arr.length - 1));
        return str.toString();
    }

}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18