344. Reverse String - jiejackyzhang/leetcode-note GitHub Wiki

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

String类题目。

先转换成Array,然后可以按照reverse Array的方法解。

public class Solution {
    public String reverseString(String s) {
        char[] array = s.toCharArray();
        int i = 0, j = array.length-1;
        while(i < j) {
            char temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
        }
        return new String(array);
    }
}