共計(jì) 3879 個字符,預(yù)計(jì)需要花費(fèi) 10 分鐘才能閱讀完成。
這篇文章主要介紹“Java 左旋轉(zhuǎn)字符串的方法是什么”,在日常操作中,相信很多人在 Java 左旋轉(zhuǎn)字符串的方法是什么問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java 左旋轉(zhuǎn)字符串的方法是什么”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
public class LeftRotateString {
/**
* Q 26 左旋轉(zhuǎn)字符串
* 題目:定義字符串的左旋轉(zhuǎn)操作:把字符串前面的若干個字符移動到字符串的尾部。 * 如把字符串 abcdef 左旋轉(zhuǎn) 2 位得到字符串 cdefab。 * 請實(shí)現(xiàn)字符串左旋轉(zhuǎn)的函數(shù)。要求時間對長度為 n 的字符串操作的復(fù)雜度為 O(n),輔助內(nèi)存為 O(1)。 */
public static void main(String[] args) {
String data = abcdef
String re = leftRotateString(data, 2);
System.out.println(re);
}
/*
* abcdef- ab.cdef- ba.fedc- cdefab
*/
public static String leftRotateString(String str, int n) { if (str == null || str.length() == 0) {
return str;
}
if (n = 0 || n = str.length()) {
return str;
}
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) {
/*
* of course we can do it like this: StringBuilder sb=new
* StringBuilder(str); sb.reverse().toString(); but we are learning
* algorithm so let s reinvent the wheel .
*/
if (begin = end) {
return;
}
for (int i = begin, j = end; i j; i++, j--) { char tmp = letters[i];
letters[i] = letters[j];
letters[j] = tmp;
}
System.out.println(--- +new String(letters)+ ---
/**
*
---bacdef---
---bafedc---
---cdefab---
cdefab
*
*/
}
================================
public class ShiftLeft {
static String str;
public ShiftLeft(String str){
this.str = str;
}
public static void main(String[] args) {
ShiftLeft sl = new ShiftLeft( abcdefghi
System.out.printf(sl.shift(12));
* abcdef 循環(huán)左移兩位得到 cdefab
*1 暴力求解, 將左移字母的暫存臨時變量中, 將其他字母移位, 再將臨時變量, 補(bǔ)充到移位后字符串的后面
*2 ba ihgfedc - cdefghiab
*
*
*/
private String shift(int digits) { if (digits == 0){
return str;
} else if (digits 0){ digits = digits % str.length(); // 是保證循環(huán)移位的關(guān)鍵, 可以輸入大于字符串長度的移位
String left = reverse(str.substring(0,digits));
String right = reverse(str.substring(digits));
System.out.println(left: +left);
System.out.println(right: +right);
return reverse(left + right);
} else { // 0
digits = -digits;
digits = digits % str.length(); // 同上
return shift(str.length() - digits);
}
}
private static String reverse(String s) {
int start = 0;
int end = s.length() - 1;
char[] temp = new char[s.length()];
for (int i = 0; i temp.length; i++){ temp[start] = s.charAt(end);
temp[end] = s.charAt(start);
// s.charAt(start) = s.charAt(end); 這樣寫是有問題的
start++;
end--;
}
return String.valueOf(temp);
}
=====================
package com.lifeibigdata.algorithms.blog;
* Q 26 左旋轉(zhuǎn)字符串
* 題目:定義字符串的左旋轉(zhuǎn)操作:把字符串前面的若干個字符移動到字符串的尾部。 * 如把字符串 abcdef 左旋轉(zhuǎn) 2 位得到字符串 cdefab。 * 請實(shí)現(xiàn)字符串左旋轉(zhuǎn)的函數(shù)。要求時間對長度為 n 的字符串操作的復(fù)雜度為 O(n),輔助內(nèi)存為 O(1)。 */
public static void main(String[] args) {
String data = abcdef
String re = leftRotateString(data, 2);
System.out.println(re);
}
/*
* abcdef- ab.cdef- ba.fedc- cdefab
*/
public static String leftRotateString(String str, int n) { if (str == null || str.length() == 0) {
return str;
}
if (n = 0 || n = str.length()) {
return str;
}
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){ public static void reverseString(char[] letters, int begin, int end) {
/*
* of course we can do it like this: StringBuilder sb=new
* StringBuilder(str); sb.reverse().toString(); but we are learning
* algorithm so let s reinvent the wheel .
*/
if (begin = end) {
return;
}
for (int i = begin, j = end; i j; i++, j--) { char tmp = letters[i];
letters[i] = letters[j];
letters[j] = tmp;
}
System.out.println(--- +new String(letters)+ ---
/**
*
---bacdef---
---bafedc---
---cdefab---
cdefab
*
*/
}
}
到此,關(guān)于“Java 左旋轉(zhuǎn)字符串的方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
正文完