【简单算法】12.反转字符串

题目:

请编写一个函数,其功能是将输入的字符串反转过来。

示例:

输入:s = "hello"
返回:"olleh"

1.解题思路:

本题非常简单,直接前后交换即可

class Solution {
public:
    string reverseString(string s) {
        int i = 0;
        int j = s.size()-1;
        
        while(i < j){
            char c = s[i];
            s[i] = s[j];
            s[j] = c;
            i++;
            j--;
        }
        
        return s;
    }
};

猜你喜欢

转载自www.cnblogs.com/mikemeng/p/8983803.html