题目描述
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
1 /** 2 * 3 * @author gentleKay 4 * 题目描述 5 * 汇编语言中有一种移位指令叫做循环左移(ROL), 6 * 现在有个简单的任务,就是用字符串模拟这个指令的运算结果。 7 * 对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。 8 * 例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果, 9 * 即“XYZdefabc”。是不是很简单?OK,搞定它! 10 */ 11 12 public class Main42 { 13 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 String str = "abcXYZdef"; 17 int n = 3; 18 System.out.println(Main42.LeftRotateString(str, n)); 19 } 20 21 public static String LeftRotateString(String str,int n) { 22 if (str.length() == 0) { 23 return ""; 24 } 25 String sFront = str.substring(0, n); 26 String sBehind = str.substring(n); 27 String sAll = sBehind.concat(sFront); 28 return sAll; 29 } 30 31 }