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; }};新聞熱點
疑難解答