package test;
public class ReverseSentenceUpdate {
public String reverse(String sentence){
if(sentence == null || "".equals(sentence)){
return null;
}
String string = "";
for(int i=0; i<sentence.length(); ++i){
string =sentence.charAt(i)+""+string;
}
return string;
}
public void leftRotateString(String word,int n){
String before = word.substring(0, n);
String end = word.substring(n);
before = reverse(before);
end = reverse(end);
before = before+end;
before = reverse(before);
System.out.println(before);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String string = "abcdefg";
int n = 2;
ReverseSentenceUpdate reverseSentenceUpdate = new ReverseSentenceUpdate();
reverseSentenceUpdate.leftRotateString(string, n);
}
}
剑指Offer 42题 左旋转字符串 Java版
最新推荐文章于 2024-08-21 11:22:17 发布