Reverse String

题目:

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

分析:

最基本的反转字符串,没什么好说的,题目来自leetcode, lintcode上面没有。

解法:

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

        return s;
    }
};

results matching ""

    No results matching ""