LeetCode 344. Reverse String

    xiaoxiao2021-03-26  28

    描述 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; } };
    转载请注明原文地址: https://ju.6miu.com/read-662639.html

    最新回复(0)