LeetCode 344. Reverse String 题解

Total Accepted: 34109 Total Submissions: 58119 Difficulty: Easy
Write a function that takes a string as input and returns the string reversed.

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

public class Solution {
    public String reverseString(String s) {
        
        char[] charArray = s.toCharArray();
        int i = 0, j = charArray.length - 1;
        for ( i = 0; i < j; i++, j--){
            char temp = charArray[i];
            charArray[i] = charArray[j];
            charArray[j] = temp;
        }
        
        // s = String.valueOf(charArray);
        return String.valueOf(charArray);
    }
}
posted @ 2016-06-14 11:13  Live and Learn  阅读(92)  评论(0编辑  收藏  举报