剑指offer42——翻转字符串vs左旋

来源:互联网 发布:淘宝免单优惠白菜群 编辑:程序博客网 时间:2024/04/29 11:12

剑指offer中的思路运用三次翻转。
翻转字符串的代码:

    void Reserve(string& str,int Begin,int End) {        char temp;        while(Begin<End) {            char temp = str[Begin];            str[Begin] = str[End];            str[End] = temp;            Begin++;            End--;        }    }

实际还有更简单的方法:

class Solution {public:    string LeftRotateString(string str, int n) {        int length = str.length();        if(length==0||n>length)            return "";        str += str;        return str.substr(n,length);    }};
0 0