43 string left rotation

Topics requirements: assembly language instruction called a cycle there is a shift left (the ROL), and now there is a simple task, this instruction is simulated by the string operation result. For a given character sequence S, you put its left circle after K-bit serial output. For example, the character sequence S = "abcXYZdef", rotated left required output result after three, i.e. "XYZdefabc". Is not it simple? OK, get it!

这道题考的核心是应聘者是不是可以灵活利用字符串翻转。假设字符串abcdef,n=3,设X=abc,Y=def,所以字符串可以表示成XY,如题干,问如何求得YX。假设X的翻转为XT,XT=cba,同理YT=fed,那么YX=(XTYT)T,三次翻转后可得结果。
 
. 1  public  class Solution {
 2      // First write reversing function
 . 3      // Java String inverted in respect of, mostly by means of a char [] to operate because there is no string setCharAt a method similar to
 4      // but there is StringBuffer 
. 5      public  void Reverse ( char [] C, int Start, int End) {
 . 6          the while (Start < End) {
 . 7              char TEMP = C [Start];
 . 8              C [Start] = C [End];
 . 9              C [End] = TEMP;
 10              Start ++ ;
 . 11             end-- ;
 12 is          }
 13 is      }
 14      public String LeftRotateString (STR String, int n-) {
 15          char [] = C str.toCharArray ();
 16          // considered special cases 
. 17          IF (c.length <n-) return "" ;
 18          // three flip thing 
. 19          Reverse (C, 0,. 1-n- );
 20 is          Reverse (C, n-,-c.length. 1 );
 21 is          Reverse (C, 0,-c.length. 1 );
 22 is          return  new new String (c); //This method is excellent, the char [] is converted into String 
23 is      }
 24 }

 

 
 
 

Guess you like

Origin www.cnblogs.com/shareidea94/p/11220705.html