[LeetCode] Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题目要求反转一个字符串,利用双指针求解。
class Solution { public: string reverseString(string s) { int left = 0, right = s.size() - 1; while (left <= right) swap(s[left++], s[right--]); return s; } }; // 9 ms
相关题目:Reverse String II