匯編語言中有一種移位指令叫做循環左移(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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM