《剑指offer》刷题笔记(知识迁移能力):左旋转字符串
2017-12-14 14:52
267 查看
《剑指offer》刷题笔记(知识迁移能力):左旋转字符串
转载请注明作者和出处:http://blog.csdn.net/u011475210代码地址:https://github.com/WordZzzz/Note/tree/master/AtOffer
刷题平台:https://www.nowcoder.com/
题 库:剑指offer
编 者:WordZzzz
剑指offer刷题笔记知识迁移能力左旋转字符串
题目描述
解题思路
C版代码实现
题目描述
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!解题思路
和上一道题一样,我们根据n将字符串分为两部分,这两部分先自己反转,然后再一同反转。C++版代码实现
class Solution { public: string LeftRotateString(string str, int n) { int length = str.size(); if(length < 0) return NULL; if(length >= 0 && n >= 0 && n <= length){ int pFirstStart = 0; int pFirstEnd = n - 1; int pSecondStart = n; int pSecondEnd = length - 1; // 翻转字符串的前面n个字符 reverseWord(str, pFirstStart, pFirstEnd); // 翻转字符串的后面部分 reverseWord(str, pSecondStart, pSecondEnd); // 翻转整个字符串 reverseWord(str, pFirstStart, pSecondEnd); } return str; } void reverseWord(string &str, int begin, int end){ while(begin < end) swap(str[begin++], str[end--]); } };
系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~
完的汪(∪。∪)。。。zzz
相关文章推荐
- 【剑指offer】6.3知识迁移能力——面试题42:翻转单词顺序VS左旋转字符串
- 《剑指offer》刷题笔记(知识迁移能力):平衡二叉树
- 《剑指offer》刷题笔记(知识迁移能力):和为S的两个数字
- (C++)剑指offer-43:左旋转字符串(知识迁移能力)
- 《剑指offer》刷题笔记(知识迁移能力):和为S的连续正数序列
- 《剑指offer》刷题笔记(知识迁移能力):翻转单词顺序列
- 剑指offer 42题 【知识迁移能力】左旋转字符串
- 《剑指offer》刷题笔记(知识迁移能力):数字在排序数组中出现的次数
- 《剑指offer》刷题笔记(知识迁移能力):二叉树的深度
- 《剑指offer》刷题笔记(知识迁移能力):数组中只出现一次的数字
- 【剑指Offer-知识迁移能力】左旋字符串
- 剑指offer 算法 (知识迁移能力2)
- 剑指offer 6.3 知识迁移能力8-左旋字符串
- 剑指offer 42题 【知识迁移能力】翻转单词顺序列
- 平衡二叉树(剑指offer)知识迁移能力
- 【剑指offer】6.3知识迁移能力——面试题40:数组中只出现一次的数字
- 剑指offer之左旋转字符串
- 剑指offer 6.3 知识迁移能力5-和为s的两个数字
- 剑指offer——左旋转字符串
- (C++)剑指offer-40:数组中只出现一次的数字(知识迁移能力)