Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Given s = "hello", return "olleh".
---------------------------------------------------------------------------------------------
This is one of the easiest problem in leetcode.
To reverse the string, all we need to do is swap the first character with the last one and go on.
class Solution { public: string reverseString(string s) { int left = 0, right = s.size() - 1; while (left < right) { char t = s[left]; s[left++] = s[right]; s[right--] = t; } return s; } };
0 意見:
張貼留言