请编写一个函数,其功能是将输入的字符串反转过来。
示例:
输入: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; } };
原文地址:https://www.cnblogs.com/xidian2014/p/8836357.html
时间: 2024-10-21 19:09:23