题解 | #左旋转字符串#
左旋转字符串
https://www.nowcoder.com/practice/12d959b108cb42b1ab72cef4d36af5ec
class Solution { public: string LeftRotateString(string str, int n) { // write code here if (str.empty()) return "";//字符串为空,直接放回 "" int m = str.size(); int val = n % m;// 求余,算一下最后一个周期,需要移动几位 string tmp_1(str.begin(), str.begin()+val); string tmp_2(str.begin()+val, str.end()); //cout << tmp_1 << " " << tmp_2; return tmp_2+tmp_1;//把移动后结果拼接返回 } };
挤挤刷刷! 文章被收录于专栏
记录coding过程