题意:
将一个字符串str的内容颠倒过来,并输出。
方法一:
C++函数
思路:调用C++函数 reverse() 实现反转字符串。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin,s);
reverse(s.begin(),s.end());//反转字符串
cout << s << endl;
return 0;
}
时间复杂度:
空间复杂度:![]()
方法二:
模拟
思路:遍历字符串,并交换对称的字符。
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
getline(cin,s);
//反转字符串
int len=s.size();
for(int i=0;i<len/2;i++){
swap(s[i],s[len-1-i]);//交换
}
cout << s << endl;
return 0;
}
时间复杂度:
空间复杂度:![]()



京公网安备 11010502036488号