Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 608 Bytes

File metadata and controls

34 lines (28 loc) · 608 Bytes

Reverse String

Solution 1

In-place

/**
 * Question   : 344. Reverse String
 * Complexity : Time: O(n) ; Space: O(1)
 * Topics     : Array
 */
class Solution {
    public void reverseString(char[] s) {
        if (s == null || s.length == 0) {
            return;
        }

        int left = 0, right = s.length - 1;

        while (left <= right) {
            swap(s, left, right);
            left++;
            right--;
        }
    }

    private void swap(char[] s, int left, int right) {
        char temp = s[left];
        s[left] = s[right];
        s[right] = temp;
    }
}