题解 | #字符逆序#

字符逆序

http://www.nowcoder.com/practice/cc57022cb4194697ac30bcb566aeb47b

题意:
        将一个字符串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;
}



时间复杂度:
空间复杂度:



全部评论

相关推荐

03-07 13:32
门头沟学院 C++
未来可欺a:读研吧,这简历只适合学历高的,本科大概率只能干开发,你这个简历开发不匹配,算法和深度学习的话学历又不够
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务