-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151.java
More file actions
27 lines (26 loc) · 738 Bytes
/
151.java
File metadata and controls
27 lines (26 loc) · 738 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
public class Solution {
public static String reverseWords(String s) {
while (s.startsWith(" ")){ s = s.substring(1);}
String[] strs = s.split(" ");
int len = strs.length;
String tmp;
for (int i = 0; i < (int)((len+1)/2); i++){
tmp = strs[len-1-i];
strs[len-1-i] = strs[i];
strs[i] = tmp;
}
return join(" ",strs);
}
public static String join(String join, String[] strAry){
StringBuffer sb=new StringBuffer();
for(int i=0;i<strAry.length;i++){
if (strAry[i].length() == 0){continue;}
if(i==(strAry.length-1)){
sb.append(strAry[i]);
}else{
sb.append(strAry[i]).append(join);
}
}
return new String(sb);
}
}