344. Reverse String

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[] chars = s.toCharArray();
        int l =0, r = chars.length-1;
        while(l < r){
            char tmp = chars[l];
            chars[l] = chars[r];
            chars[r] = tmp;
            l++;
            r--;
        }

        return new String(chars);

    }
}

results matching ""

    No results matching ""