[LeetCode] 151. Reverse Words in a String(Kotlin)

0

LeetCode

목록 보기
13/58
post-thumbnail
post-custom-banner

[LeetCode] 151. Reverse Words in a String(Kotlin)

풀이

  • trim(): 문자열 앞,뒤 공백 제거
class Solution {
    fun reverseWords(s: String): String {
        var strArray = s.split(' ').map{it.toString().trim()}.filter{it != ""}

        var answer = ""
        for(i in strArray.size-1 downTo 1){
            answer += strArray[i]
            answer += " "
        }
        answer += strArray[0]
        return answer
    }
}
profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글