首页 > 代码库 > reverse string | leetcode

reverse string | leetcode

思路:在原来的字符串后面添加上strlen-1个字符,返回

 

 

class Solution {  
public:  
    string reverseString(string s) {  
        unsigned int strlen;  
        int i;  
        // char temp;  
          
        strlen = s.size();  
        if (strlen > 1) {  
            for (i = strlen - 2; i >= 0 ; i--)   {  
                // temp = s[i];  
                s += s[i];  
            }  
            return s.substr(strlen - 1 , strlen + strlen - 2);  
        }  
        else  
            return s;  
    }  
}; 

 

reverse string | leetcode