File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ ##### 题目
2
+ 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
3
+
4
+ 示例 1:
5
+
6
+ 输入: "Let's take LeetCode contest"
7
+ 输出: "s'teL ekat edoCteeL tsetnoc"
8
+
9
+ 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
10
+ ##### 代码
11
+
12
+ ```
13
+ public class Solution {
14
+ public String reverseWords(String s) {
15
+
16
+ if(s == null){ return null;}
17
+
18
+ String[] strChar=s.split(" ");
19
+ int size=strChar.length;
20
+ //reverse every vocabulary
21
+ String reverse="";
22
+ for (int i=0; i<size; ++i) {
23
+ strChar[i] = new StringBuilder(strChar[i]).reverse().toString();
24
+ }
25
+ //linking every vocabulary
26
+ StringBuilder result = new StringBuilder();
27
+ for (String st : strChar) {
28
+ result.append(st + " ");
29
+ }
30
+ return result.toString().trim();
31
+ }
32
+ }
33
+ ```
You can’t perform that action at this time.
0 commit comments