Friday, April 22, 2016

344. Reverse String E

Q:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
A:

class Solution {
public:
    void reverseString(vector<char>& s) {
        int i =0, j = s.size()-1;
        while(i<j){
            swap(s[i],s[j]);
            ++i;
            --j;
        }
    }
};


No comments:

Post a Comment