Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

class Solution { public: string reverseString(string s) { int i = 0, j = s.size() - 1; while(i < j){ swap(s[i++], s[j--]); } return s; } };

class Solution { public: string reverseString(string s) { for(int i=0,j=s.length()-1; i<j; ++i,--j) swap(s[i], s[j]); return s; } };

Hi, if s is empty, j would be a big number in some system.

s.size() is unsigned int and 0 - 1 = -1 in some system. But 0 - 1 = Max Unsigned int in other.

Another solution:

class Solution { public: string reverseString(string s) { int n = s.size(); for(int i = 0; i < n/2; i++) { swap(s[i], s[n - 1 - i]); } return s; } };

Same solution in python,

class Solution(object): def reverseString(self, s): """ :type s: str :rtype: str """ i, j =0, len(s)-1 ls = list(s) while i <j: ls[i], ls[j] = ls[j], ls[i] i+=1 j-=1 return "".join(str(x) for x in ls)

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.