public class leftrotatestring
/** abcdef->ab.cdef->ba.fedc->cdefab
*/public static string leftrotatestring(string str, int n)
if (n <= 0 || n >= str.length())
int begin = 0;
int end = str.length() - 1;
char letters = str.tochararray();#考慮引用傳遞可以修改值
reversestring(letters, begin, n - 1);
reversestring(letters, n, end);
reversestring(letters, begin, end);
return new string(letters);
}public static void reversestring(char letters, int begin, int end)
for (int i = begin, j = end; i < j; i++, j--)
system.out.println("---"+new string(letters)+"---");
/***
---bacdef---
---bafedc---
---cdefab---
cdefab**/
}}******************************==
public class shiftleft
public static void main(string args)
/*** abcdef迴圈左移兩位得到cdefab
*1 暴力求解,將左移字母的暫存臨時變數中,將其他字母移位,再將臨時變數,補充到移位後字串的後面
*2 ba ihgfedc -> cdefghiab**
*/private string shift(int digits) else if (digits > 0) else
}private static string reverse(string s)
return string.valueof(temp);
}}********************=
package com.lifeibigdata.algorithms.blog;
public class leftrotatestring
/** abcdef->ab.cdef->ba.fedc->cdefab
*/public static string leftrotatestring(string str, int n)
if (n <= 0 || n >= str.length())
int begin = 0;
int end = str.length() - 1;
char letters = str.tochararray();
reversestring(letters, begin, n - 1);
reversestring(letters, n, end);
reversestring(letters, begin, end);
return new string(letters);
}// public static string reversestring(string str,int begin,int end)
for (int i = begin, j = end; i < j; i++, j--)
system.out.println("---"+new string(letters)+"---");
/***
---bacdef---
---bafedc---
---cdefab---
cdefab**/
}}
左旋轉字串
題目 定義字串的左旋轉操作 把字串前面的若干個字元移動到字串的尾部。如把字串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...
左旋轉字串
如abc,左旋1得到bca,左旋2得到cab o n k 的演算法 include using namespace std include include include include include include include include include include int main...