左旋轉字串

2021-06-23 03:38:15 字數 802 閱讀 9190

如abc,左旋1得到bca,左旋2得到cab

o(n*k)的演算法:

#include using namespace std;

#include #include #include #include #include #include #include #include #include #include int main()

for(int i = 1;i <= k;i++)

}printf("%s\n",s);

}system("pause");

}

o(n)的演算法,注意reverse函式,思路:先翻轉【0,k-1】區間的字串,再翻轉【k,len-1】的字串,最後整體翻轉【0,len-1】的字串,呼叫三次reverse() 函式

#include using namespace std;

#include #include #include #include #include #include #include #include #include #include void reverse(char s,int beg,int end)

}int main()

len = strlen(st);

reverse(st,0,k-1);

reverse(st,k,len-1);

reverse(st,0,len-1);

printf("%s\n",st);

}system("pause");

}



左旋轉字串

題目 定義字串的左旋轉操作 把字串前面的若干個字元移動到字串的尾部。如把字串abcdef左旋轉2位得到字串cdefab。請實現字串左旋轉的函式。要求時間對長度為n的字串操作的複雜度為o n 輔助記憶體為o 1 思想 旋轉三次 include include using namespace std v...

左旋轉字串

package com.string 旋轉字串 q 26 左旋轉字串 題目 定義字串的左旋轉操作 把字串前面的若干個字元移動到字串的尾部。如把字串abcdef左旋轉2位得到字串cdefab。請實現字串左旋轉的函式。要求時間對長度為n的字串操作的複雜度為o n 輔助記憶體為o 1 public cla...

左旋轉字串

組合語言中有一種移位指令叫做迴圈左移 rol 現在有個簡單的任務,就是用字串模擬這個指令的運算結果。對於乙個給定的字串行s,請你把其迴圈左移k位後的序列輸出。例如,字串行s abcxyzdef 要求輸出迴圈左移3位後的結果,即 xyzdefabc 是不是很簡單?ok,搞定它!public stati...