344. Reverse String LeetCode Problem Solving

344. Reverse String

Difficulty:Easy 

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

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

 

Code:

 

public class Solution {
    public String reverseString(String s) {
        char [] sArray = s.toCharArray ();
        int length = sArray.length;     
        for (int i = 0; i < length/2; i ++) {
            char temp = sArray[i];
            int position = length - 1 - i;
            sArray [i] = sArray [position];
            sArray[position] = temp;
        }
        return new String(sArray);
    }
}
 I always feel that the efficiency of this code is a bit low. If there is a faster solution, welcome to exchange.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326896258&siteId=291194637