344. Reverse String

Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".

Solution1:前后two pointers对换

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);
    }
}

你可能感兴趣的:(344. Reverse String)