请编写一个函数,其功能是将输入的字符串反转过来。
示例:
输入:s = "hello"
返回:"olleh"
详见:https://leetcode.com/problems/reverse-string/description/
C++:

class Solution {
public:
    string reverseString(string s) {
        int n=s.size();
        if(n==0||s.empty())
        {
            return "";
        }
        int left=0,right=n-1;
        while(left<right)
        {
            char tmp=s[left];
            s[left]=s[right];
            s[right]=tmp;
            ++left;
            --right;
        }
        return s;
    }
};

 

相关文章:

  • 2021-09-23
  • 2022-02-19
  • 2022-12-23
  • 2022-01-22
  • 2022-01-22
  • 2021-07-31
  • 2021-10-06
猜你喜欢
  • 2021-08-22
  • 2021-08-14
  • 2021-06-13
  • 2021-09-22
  • 2022-01-02
  • 2021-06-14
相关资源
相似解决方案