No. 19 - Left Rotation of String
来源:互联网 发布:网络爬虫书籍推荐 编辑:程序博客网 时间:2024/04/30 16:02
No. 19 - Left Rotation of String
Problem: Left rotation of a string is to move some leading characters to its tail. Please implement a function to rotate a string.
For example, if the input string is “abcdefg” and a number 2, the rotated result is “cdefgab”.
Analysis: It looks difficult to get rules of left rotation on a string. Fortunately, the 7th problem in this series “Reverse Words in a Sentence” can give us some hints.
If we input a sentence with two words “hello world” for the problem “Reverse Words in a Sentence”, the reversed result should be “world hello”. It is noticeable that the result “world hello” can be viewed as a rotated result of “hello world”. It becomes “world hello” when we move some leading characters of string “hello world” to its ending. Therefore, this problem is quite similar to problem “Reverse Words in a Sentence”.
Let us take a string “abcdefg” as an example. We divide it into two parts: the first part contains the two leading characters “ab”, and the second part contains all other characters “cdefg”. We firstly reverse these two parts separately, and the whole string becomes “bagfedc”. It becomes “cdefgab” if we reverse the whole string, which is the expected result of left rotation with 2.
According to the analysis above, we can see that left rotation of a string can be implemented calling a Reverse function three times to reverse a segment or whole string. The sample code is shown below:
char* LeftRotateString(char* pStr, int n)
{
if(pStr != NULL)
{
int nLength = static_cast<int>(strlen(pStr));
if(nLength > 0 && n > 0 && n < nLength)
{
char* pFirstStart = pStr;
char* pFirstEnd = pStr + n - 1;
char* pSecondStart = pStr + n;
char* pSecondEnd = pStr + nLength - 1;
// Reverse the n leading characters
Reverse(pFirstStart, pFirstEnd);
// Reverse other characters
Reverse(pSecondStart, pSecondEnd);
// Reverse the whole string
Reverse(pFirstStart, pSecondEnd);
}
}
return pStr;
}
The function Reverse is shown in “Reverse Words in a Sentence”, so we are not going to repeat it here.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books, please contact me (zhedahht@gmail.com) . Thanks.
- No. 19 - Left Rotation of String
- string rotation (if string is the rotate of the other)
- string rotation
- sicily Left Rotation
- HackerRank-Left Rotation
- No. 47 - Search in a Rotation of an Array
- 【6】Decide if one string is a rotation of another string or not
- Lexicographically minimum string rotation
- 1.9 String Rotation
- CC150 1.9 string rotation
- no new variables on left side of :=
- Hackerrank - Game Of Rotation 题解
- no space left device
- ORA-27102:out of memory Linux-X86_64 Error: 28: No space left on device
- dbca ORA-27102: out of memory Linux-x86_64 Error: 28: No space left on device
- error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::
- ORA-27102: out of memory Linux-x86_64 Error: 28: No space left on device
- ORA-27102: out of memory Linux-x86_64 Error: 28: No space left on device
- 实习总结(四)---OA办公业务资源系统需求规格说明书
- 腾讯面试题—附答案
- DirectX游戏编程入门——第二部分(游戏编程工具箱) ——精灵编程之简介
- No. 17 - Queue Implemented with Two Stacks
- No. 18 - Reverse a Linked List
- No. 19 - Left Rotation of String
- No. 20 - Number of 1 in a Binary
- Linux学习笔记(一)
- lq51--基于8051的嵌入式实时操作系统
- hdu1068
- ObjectInputStream 和 ObjectOutputStream(对象的序列化、持久化)
- VC 多线程编程
- poj3468 A Simple Problem with Integers
- vs下创建并调用DLL--简单例子