Leetcode 344) Reverse String

https://leetcode.com/problems/reverse-string/

image

Follow up: Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

class Solution {
    public void reverseString(char[] s) {
        char tmp;
        int start=0;
        int end=s.length-1;
        while(start<=end){
            tmp = s[start];
            s[start]=s[end];
            s[end]=tmp;
            start++;end--;
        }
    }
}

© 2018. All rights reserved.

Powered by Hydejack v8.5.2