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