Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
这个题目考察O(1)空间交换数组的。
public class Solution {
public String reverseString(String s) {
char[] str = s.toCharArray();
int i =0 , j = s.length()-1;
while(i<j){
char c= str[i];
str[i] = str[j];
str[j] = c;
i++;
j--;
}
return String.valueOf(str);
}
}
时间复杂度O(N).