4.Reverse Words in a String III

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4.Reverse Words in a String III相关的知识,希望对你有一定的参考价值。

leetcode地址:https://leetcode.com/problems/reverse-words-in-a-string-iii/description/

题目:一个字符串,单词反转

Input: "Let‘s take LeetCode contest"
Output: "s‘teL ekat edoCteeL tsetnoc"
class Solution {
    public String reverseWords(String s) {
        String[] strArray = s.split(" ");
        String result = "";
        for(int i=0;i<strArray.length;i++)
        {
            result += reverse(strArray[i]);
            if(i != strArray.length)
                result += " ";
        }
        return result;
    }
    public String reverse(String originStr) 
    {
        if(originStr == null || originStr.length() <= 1) 
            return originStr;
        return reverse(originStr.substring(1)) + originStr.charAt(0);
    }
}

 

以上是关于4.Reverse Words in a String III的主要内容,如果未能解决你的问题,请参考以下文章

Reverse Words in a String

186. Reverse Words in a String II

Reverse Words in a string

LeetCode Reverse Words in a String III

leetcode557. Reverse Words in a String III

151. Reverse Words in a String