汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!

// test20.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<deque>

using namespace std;

class Solution {
public:
	string LeftRotateString(string str, int n) {
		if (str == "") return {};
		
		int len = str.length();
		int flag = n % len;
		if (flag == 0) return str;
		string roc1 = str.substr(0,flag);
		//cout << roc1 << endl;
		string rocs = str.substr(flag);
		//cout << rocs << endl;
		rocs.append(roc1);
		return rocs;
	}
};
int main()
{
	
	Solution so;
	
	//string str = so.LeftRotateString("abcXYZdef",3);
	string str = so.LeftRotateString("abcXYZdef", 0);
	cout << str << endl;
	cout << endl;
	return 0;
}
posted @ 2016-11-03 20:46  wdan2016  阅读(2034)  评论(0编辑  收藏  举报