Topic website: https://leetcode.com/problems/reverse-string/
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "Hello", Return "Olleh".
Solution:
C++:
Class Solution {public: string reversestring (string s) { int len = s.length (); char result = '; int j = 0; for (int i = len-1;i>=0;i--) { result[j] = s[i]; j + +; } return result;} ;
Python:
Method One:
Class solution (Object): def reversestring (self, s): "" " : Type s:str : Rtype:str " "" return S [::-1]
Method Two:
Class solution (Object): def reversestring (self, s): "" " : Type s:str : Rtype:str " "" t = List (s) L = Len (t) for i,j in Zip (range (l-1, 0,-1), Range (L//2)): T[i], t[j] = T[j], T[i] return "". J Oin (t)
Javascript:
/** * @param {string} S * @return {string} */var reversestring = function (s) { return S.split ("). Reverse (). Join ('); };
Leetcode 344. Reverse String