Write a function that takes a string as input and returns the string reversed.
这个题也没啥说的,按道理直接StringBuilder然后reverse一下就可以了,既然是道题,估计可能需要转成一个char array一个一个交换
class Solution {
public String reverseString(String s) {
if(s == null || s.length() <= 1)
return s;
char[] cs = s.toCharArray();
int i = 0;
int j = cs.length - 1;
while(i < j) {
swap(cs, i++, j--);
}
return String.valueOf(cs);
}
public void swap(char[] cs, int i, int j) {
char temp = cs[i];
cs[i] = cs[j];
cs[j] = temp;
}
}