leetcode 334 ----字符串翻转

来源:互联网 发布:js修改div背景颜色 编辑:程序博客网 时间:2024/05/01 00:31
/*leetcode 334 ----字符串翻转    解题思路:把字符串第i个和第len-1-i个位置的字符交换即可。*/#include <iostream>#include <string>using namespace std;class Solution {public:    string reverseString(string s) {        int len = s.length();        if (len==0 || len == 1)            return s;        char c;        for (int i = 0; i < len / 2; ++i)        {            c = s[i];            s[i] = s[len - 1 - i];            s[len - 1 - i] = c;        }        return s;    }};int main(){    Solution sol;    string s("abcdefg");    cout << sol.reverseString(s) << endl;    return 0;}
0 0