Skip to content

Instantly share code, notes, and snippets.

@Cee
Created May 26, 2014 02:33
Show Gist options
  • Save Cee/d0644e9db010f8779573 to your computer and use it in GitHub Desktop.
Save Cee/d0644e9db010f8779573 to your computer and use it in GitHub Desktop.
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;
}
}
@Cee
Copy link
Author

Cee commented May 26, 2014

String StringBuffer StringBuilder的差异还是很明显的。
这个解法是一个比较好的只用String的解法。
一开始自己做用了StringBuffer,单用String还是TLE了=。=

@Cee
Copy link
Author

Cee commented May 26, 2014

还有String的几个函数别晕了。。
charAt和length()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment