[LeetCode reverse text]

【problem】

Write a function, its role is to input string reversed. 
Example 1 : 

Input: " Hello "    
Output: " olleh " 
Example 2 : 

Input: " A man, A Plan, A Canal: Panama "    
Output: " amanaP: lanac A, the NALP A, Nam A "

[Thinking] to go directly to the middle from both ends, while both sides of the character can swap

【answer】

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

        return s;
    }
};

Guess you like

Origin www.cnblogs.com/zhudingtop/p/11688033.html