ReverseString2 [source code]
public class ReverseString2 {
public String reverseString(String s) {
char[] words = s.toCharArray();
int i = 0;
int j = words.length - 1;
while ( i<j ) {
char tmp = words[j];
words[j] = words[i];
words[i] = tmp;
i++;
j--;
}
return new String(words);
}
}
这个版本就直接接受不超时了,不知道什么原理. 不过 string 和char array之间的转换还是要学会; 时间是3ms, 55%, 凑合差不多了, 可以不折腾了.
Problem Description
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".