-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestStringReverse.java
More file actions
38 lines (33 loc) · 960 Bytes
/
TestStringReverse.java
File metadata and controls
38 lines (33 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
public class TestStringReverse {
static class Solution{
public static String reverse(String str){
if(str ==null||str.length()<1){
return str;
}
List<String> list = new ArrayList<>();
for(int i=0,j = 0; i<str.length();i++){
if( ' ' == str.charAt(i)){
list.add(str.substring(j, i));
j = i;
while (i<str.length() && ' '== str.charAt(i)){
i++;
}
list.add(str.substring(j,i));
j = i;
}
}
String res = "";
Collections.reverse(list);
for (String s : list) {
res+=s;
}
return res;
}
}
public static void main(String[] args) {
String str = " hello my name is zhangsan ";
String strReversed = Solution.reverse(str);
System.out.println(strReversed);
}
}