zoukankan      html  css  js  c++  java
  • [LeetCode]344. 反转字符串

    题目


      请编写一个函数,其功能是将输入的字符串反转过来。
      示例:

    输入:s = "hello"
    返回:"olleh"

    思路


      从前往后遍历字符串,遍历的过程中将第i个字符与第length-i-1个字符交换即可

    代码


    #include<iostream>
    using namespace std;
    class Solution {
    public:
        string reverseString(string s) {
            /*当然最简单的办法就是调用STL中的reverse函数拉,这里就不用这样的方法了
            reverse(s.begin(),s.end());*/
            string::size_type mid=(s.end()-s.begin())/2;
            for (auto iterBegin = s.begin(); iterBegin != s.begin() + mid; iterBegin++)
            {
                swap(*iterBegin,*( s.begin()+(s.end() - iterBegin-1)));
            }
            return s;
        }
    };
    https://github.com/li-zheng-hao
  • 相关阅读:
    bzoj1221
    hdu3377
    bzoj3930
    bzoj3976
    bzoj4237
    fzu1977
    hdu1693
    ural1519
    bzoj1264
    回答自己的提问
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053689.html
Copyright © 2011-2022 走看看