反转字符串中的字符

class Solution {
    public String reverseWords(String s) {
        StringBuffer ret = new StringBuffer();
        int length = s.length();
        int i = 0;
        while(i < length){
            int start = i;
            while(i<length && s.charAt(i) != ' '){
                i++;
            }
            for(int pos = start; pos < i; pos++){
                ret.append(s.charAt(start + i - 1 - pos));
            }
            while(i<length && s.charAt(i) == ' '){
                i++;
                ret.append(' ');
            }
        }
        return ret.toString();
    }

  https://blog.csdn.net/qq_57314342/article/details/123938734

posted @ 2023-10-29 00:05  樱圃  阅读(5)  评论(0编辑  收藏  举报