题目描述
- Write a function that takes a string as input and returns the string reversed.
- Example:
Given s = “hello”, return “olleh”. - 地址
代码实现
- 双指针与 StringBuffer
class Solution {
public String reverseString(String s) {
if (s == null || s.length() <= 1){
return s;
}
/*
return new StringBuffer(s).reverse().toString();
*/
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length / 2 ; i++) {
char temp = chs[i];
chs[i] = chs[chs.length - i - 1];
chs[chs.length - i - 1] = temp;
}
return new String(chs);
}
}