class Solution(object): def reverseString(self, s): n = len(s) s = list(s) # return self.reverseStringRecurse(s, 0, len(s)-1) # return self.reverseString2(s) # return self.reverseString3(s) for i in range(n/2): s[i], s[~i] = s[~i], s[i] return "".join(s) def reverseString2(self, s): return s[::-1] def reverseString3(self, s): return "".join(reversed(list(s))) def reverseStringRecurse(self, s, lo=0,hi=None): if hi<=lo: return "".join(s) s[lo],s[hi] = s[hi], s[lo] return self.reverseStringRecurse(s, lo+1, hi-1)
@ahendy Great solution, the trick is i + ~i = -1, and -1 is the last index.
@churchmice much nicer than writing len(s)-i-1 :)
You didn't consider the Negative numbers, the overflow issue(over 2^31-1) and situations like "10" that ended with "0"
Did you reply to wrong question? Not sure what you mean!
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.