Title Description
There is a shift in the assembly language instruction called a rotate left (the ROL), and now there is a simple task, this instruction is simulated by the string operation result. For a given character sequence S, you put its left circle after K-bit serial output. For example, the character sequence S = "abcXYZdef", rotated left required output result after three, i.e. "XYZdefabc". Is not it simple? OK, get it!
answer:
Flip to the last:
Flip the left part and right part of the flip, then flip the whole can.
1 class Solution02 { 2 public: 3 string LeftRotateString(string str, int n) { 4 int size = str.length(); 5 if (size < 2 || n % size == 0)return str; 6 n %= size; 7 Reverse(str, 0, n - 1); 8 Reverse(str, n, size - 1); 9 Reverse(str, 0, size - 1); 10 return str; 11 } 12 private: 13 void Reverse(string &str,int L, int R) 14 { 15 while (L < R) { 16 swap(str[L], str[R]); 17 ++L, --R; 18 } 19 } 20 };