leetcode : 344. Reverse String : memory usage beats 99.55 % of javascript submissions

Posted on Updated on

Nice, another leetcode challenge where I beat most of the submissions in memory.

This is most likely because people did not pay attention to the spec which says: “Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.” I took that to mean as you can only have one extra variable so that’s what I used

var reverseString = function(s) {
    for(i=0;i<~~(s.length/2);i++){
        let temp = s[i]
        s[i]=s[s.length-1-i]
        s[s.length-1-i] = temp
    }
};

Runtime could have been better. My runtime beats 15.26 % of javascript submissions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s