Doing something you intrinsically are enthusiastic with.

2016年8月3日 星期三

Leetcode--Reverse String

凌晨3:44 Posted by Unknown No comments
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
---------------------------------------------------------------------------------------------
This is one of the easiest problem in leetcode.
To reverse the string, all we need to do is swap the first character with the last one and go on.
class Solution {
public:
    string reverseString(string s) {
        int left = 0, right = s.size() - 1;
        while (left < right) {
            char t = s[left];
            s[left++] = s[right];
            s[right--] = t;
        }
        return s;
    }
};

0 意見:

張貼留言