Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Solution1:前后two pointers對(duì)換
Time Complexity: O(N) Space Complexity: O(N)
Solution Code:
class Solution {
public String reverseString(String s) {
char[] chars = s.toCharArray();
int i = 0, j = s.length() - 1;
while (i < j) {
char tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
i++;
j--;
}
return new String(chars);
}
}