[LeetCode]字符串——反转字符串

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

C++

class Solution {
public:
    string reverseString(string s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            swap(s[left++], s[right--]);
        }
        return s;
    }
};

C

char* reverseString(char* s) {
    int t,left = 0, right = strlen(s) - 1;
    while (left < right) {
        t = s[left];
        s[left++] = s[right];
        s[right--] = t;
    }
    return s;
}

 

猜你喜欢

转载自www.cnblogs.com/moonpie-sun/p/9425522.html