题目:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题意:
写一个程序完成将输入的一个字符串反转之后返回;
思路:
定义两个下标left、right,left从开始往后,right从后往前,当left<right时依次交换即可。
代码:12ms
class Solution { public: string reverseString(string s) { int left = 0, right = s.size() - 1; while (left < right) { swap (s[left++], s[right--]); } return s; } };