首页 > 代码库 > 344. Reverse String

344. Reverse String

// My own solution
class
Solution {public: string reverseString(string s) { return string(s.crbegin(), s.crend()); }};
// My own solution
class
Solution {public: string reverseString(string s) { int n = s.size(); const char *p = s.c_str() + ((n - 1) * sizeof(char)); string str; for(int i = 0; i < n; i++){ str += *p; p = p - sizeof(char); } return str; }};
// Learning from others‘ ideas
class
Solution {public: string reverseString(string s) { int n = s.size(); for(int i = 0; i < n / 2; i++){ char tmp = s[i]; s[i] = s[n - 1 - i]; s[n - 1 - i] = tmp; } return s; }};

 

344. Reverse String