翻轉單詞順序列

題目描述

翻轉一個句子:“student. a am I”。————》 “I am a student.”

解法

用split/concat/trim等string類方法完成

public class Solution {
    public String ReverseSentence(String str) {
        if(str.trim().isEmpty()){
            return str;
        }
        String[] temp = str.split(" ");
        String result = "";
        for(int i = temp.length - 1;i >= 0;i--){
            result = result.concat(temp[i]);
            result = result.concat(" ");
        }
        result = result.trim();
        return result;
    }
}```

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章