344. Reverse String

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

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

class Solution {
    public String reverseString(String s) {
        char[] ch = s.toCharArray();
        int start = 0;
        int end = s.length() - 1;
        while (start < end) {
            char temp = ch[start];
            ch[start] = ch[end];
            ch[end] = temp;
            start++;
            end--;
        }
        return new String(ch);
    }
}
posted @ 2019-04-09 17:06  MarkLeeBYR  阅读(83)  评论(0)    收藏  举报