Created
May 26, 2014 02:33
-
-
Save Cee/d0644e9db010f8779573 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) { | |
s = s.trim(); | |
int n = s.length(); | |
String str = ""; | |
while(s.lastIndexOf(" ") != -1){ | |
int index = s.lastIndexOf(" "); | |
str = str + s.substring(index + 1, n) + " "; | |
s = s.substring(0, index).trim(); | |
n = s.length(); | |
} | |
if(n > 0) | |
str = str + s.substring(0, n); | |
return str; | |
} | |
} |
还有String的几个函数别晕了。。
charAt和length()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
String StringBuffer StringBuilder的差异还是很明显的。
这个解法是一个比较好的只用String的解法。
一开始自己做用了StringBuffer,单用String还是TLE了=。=