Discuss Pick One
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
注意runtime error !!如果只是遍历会超时 下面注释掉的就是超时的方法
class Solution {
public String reverseString(String s) {
// if(s==null||s.length()==0)
// return "";
// String res ="";
// for(int i=s.length()-1;i>=0;i--){
// res = res+s.charAt(i);
// }
// return res;
char[] arr = s.toCharArray();
int len=s.length();
for(int i=0;i<len/2;i++){
char temp = arr[i];
arr[i]=arr[len-1-i];
arr[len-1-i]=temp;
}
return new String(arr);
}
}