Write a function that takes a string as input and returns the string reversed.
Example: Given s = “hello”, return “olleh”.
Subscribe to see which companies asked this question.
Show Tags Show Similar PRoblems 解法1:
class Solution {public: string reverseString(string s) { int left = 0, right = s.size()-1; while (left<right) { swap(s[left++] , s[right--]);//运用交换函数,逐一交换位置 } return s; }};解法2:
class Solution {public: string reverseString(string s) { int left = 0, right = s.size() - 1; while (left < right) {//与上面的一个道理,只是是自己写的swap函数 char t = s[left]; s[left++] = s[right]; s[right--] = t; } return s; }};新闻热点
疑难解答