描述 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) { const int n = s.size(); for (size_t i = 0; i < n / 2; ++i) { swap (s[i], s[n - i - 1]); } return s; }};新聞熱點
疑難解答