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);
    }
}
时间: 2024-10-19 01:02:14

4.Reverse Words in a String III的相关文章

557. Reverse Words in a String III【easy】

557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output:

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

ledecode Reverse Words in a String III

557. Reverse Words in a String III Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

leetcode_557 Reverse Words in a String III(String)

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

LeetCode 557. Reverse Words in a String III

晚饭后写了一题.当然,我这种菜鸟是从easy开始写的...发现leetcode也是有bug的 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" O

Leetcode - 557. Reverse Words in a String III (C++) stringstream

1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是要注意空格的影响.比方说,string首尾和单词之间可能有一或多个空格.看到有人逐个对空格判断,但是我觉得逐个判断其实稍微容易出错(当然如果非常熟悉的话就完全无所谓啦),我的一个简单想法是用stringstream. PS:不熟悉stringstream的朋友可以看看链接的文档. 3. 代码 cla

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Inpu

LeetCode解题思路:557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"