1. 程式人生 > >C++ Leetcode初級演算法之反轉字串

C++ Leetcode初級演算法之反轉字串

編寫一個函式,其作用是將輸入的字串反轉過來。

示例 1:

輸入: “hello”
輸出: “olleh”
示例 2:

輸入: “A man, a plan, a canal: Panama”
輸出: “amanaP :lanac a ,nalp a ,nam A”

class Solution {
public:
    string reverseString(string s) {
        int count = s.size();
        for(int i=0;i<count/2;i++)
            swap(s[i],s[count-i-1]);
        return s;
    }
};