leetCode:翻转字符串里的单词(java)

leetCode:翻转字符串里的单词(java)_第1张图片

public String reverseWords(String s) {
        String[] str = s.split(" ");
        int length = str.length;
        for (int i = 0; i < (length / 2+length % 2); i++) {
            String temp = str[i];
            str[i] = str[length-1-i];
            str[length-1-i] = temp;
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0 ; i < str.length ; i++){
            if(str[i].equals("")){
                continue;
            }
            sb.append(str[i]).append(" ");
        }
        if(sb.length() != 0)
            return sb.substring(0,sb.length()-1);
        return "";
    }

 

你可能感兴趣的:(leetCode:翻转字符串里的单词(java))