Created
November 5, 2024 03:04
-
-
Save sAVItar02/150cdb4e7e8ff1e3f18c6f83104f6bcf to your computer and use it in GitHub Desktop.
Reverse String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {character[]} s | |
* @return {void} Do not return anything, modify s in-place instead. | |
*/ | |
var reverseString = function(s) { | |
let start = 0; | |
let end = s.length - 1; | |
let temp = ""; | |
while(start <= end) { | |
temp = s[start]; | |
s[start] = s[end]; | |
s[end] = temp; | |
start++; | |
end--; | |
} | |
}; | |
// Two pointers and a temp variable approach | |
// Start poniter points to the start of the array and the end points at the end | |
// while start <= end swap the characters | |
// Time: O(n) | |
// Space: O(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment