代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public String reverseWords(String s) { | |
if (s == null) | |
return ""; | |
String res = ""; | |
int len = s.length(); | |
for (int i = 0; i < len;) { | |
String temp = ""; | |
while (i < len && s.charAt(i) == ' ') | |
i++; | |
if (i == len) | |
break; | |
while (i < len && s.charAt(i) != ' ') | |
temp += s.charAt(i++); | |
res = res.length() == 0? temp: temp + " " + res; | |
} | |
return res; | |
} | |
} |
No comments:
Post a Comment