344. Reverse String

    xiaoxiao2021-03-25  184

    Write a function that takes a string as input and returns the string reversed.

    Example:

    Given s = "hello", return "olleh".

    #include <iostream> #include <string> using namespace std; class Solution { public:     string reverseString(string s) {  /*       string ss;                         for(int i=0,j=s.length()-1;i<s.length();i++,j--)         {         ss[i] = s[j]; }                  return ss;                  for(int i=0;i<ss.length();i++)         cout << ss[i];     */           int i = 0, j = s.size() - 1;         while(i < j){             swap(s[i++], s[j--]);          }                  return s;          } }; int main() { Solution s; string r; r = "hello"; cout << s.reverseString(r) << endl; return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-7748.html

    最新回复(0)