1.1 Rotation of the string

Source: Internet
Author: User

Title: Given a string, the first few characters of a string are required to be moved to the end of the string. If there is a string "abcdef", move the first 3 characters to the tail of the string to "DEFABC". Write a function to implement this function.

Solution 1: Brute Force shift

For example, to move ' a ' to the last, assign ' a ' to a temporary variable, and then move the trailing n-1 character forward by one (note that the move is done from front to back, otherwise the useful value is overwritten), and finally the word assigned value in the temporary variable is given to the last one. To move the M-bit character, call the function m again, and for a string of length n, suppose you need to move the m character to the tail, a total of m*n times to move, so the time complexity is O (MN),

The space complexity is O (1).

The reference code is as follows:

 Public classReversrString1 { Public Static voidReverseString1 (Char[] SA,intLenintm) {if(Len <=m) {System.out.println ("SA is too small!!"); }        Chart = sa[0];  for(inti = 1;i<len;i++){            Chartemp =Sa[i]; Sa[i-1] =temp; } Sa[len-1] =T; }     Public Static voidLeftrotatestring (String str,intm) {if(str = =NULL|| Str.length () = = 0) {System.out.println ("NULL"); }        intn =str.length (); Char[] sa =Str.tochararray ();  while(m-->0) {ReverseString1 (sa,n,m);    } System.out.print (SA); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intm = 4; String Str= "ABCdef";            Leftrotatestring (STR,M); }}
View Code

Solution 2: three times reversal

Idea: A string is divided into two parts, and then the two parts of the string is reversed, and finally the whole string to reverse the overall, you can solve the problem.

Give me a chestnut: input: String "abcdef", move 3-bit

Output: string "Defabc"

1. Divide the original string into two parts, X = "abc", Y = "Def"

2, Invert x get "CBA", reverse y get "fed"

3, finally will "cbafed" overall reversal, get "DEFABC"

This kind of string is divided into two parts, each reversal, and then the whole reversal method is called "three-Step inversion" method, its time complexity is O (n), the spatial complexity of O (1)

The reference code is as follows:

 Public classReverseString2 { Public Static voidReverseString (Char[] SA,intFromintTo ) {         while(from<To ) {            Chart =Sa[from]; Sa[from++] =Sa[to]; Sa[to--] =T; }    }     Public Static voidLeftrotatestring (String str,intm) {if(str = =NULL|| Str.length () = = 0) {System.out.print ("It ' s wrong!"); }        intn =str.length (); if(m>N) {m= m%N; }        Char[] sa =Str.tochararray (); ReverseString (SA,0,m-1); ReverseString (Sa,m,n-1); ReverseString (SA,0,n-1);    SYSTEM.OUT.PRINTLN (SA); }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubString str = "ABCdef"; intm = 3;    Leftrotatestring (STR,M); }}
View Code

Extrapolate

Word flipping: Enter an English sentence to reverse the order of the words in the sentence. The order of the characters within the word is required, and the words in the sentence are separated by spaces. For simplicity, punctuation is treated like ordinary letters. For example, if you enter "I am a student.", the output is "student." A am I ".

I still use three times reversal of the idea to solve the problem, mainly to consolidate the previous study reversal thinking. There are many online Daniel use StringBuffer method can also be convenient to solve the problem.

Idea: The whole character is now reversed, then each word is reversed

The time complexity is O (n) and the spatial complexity is O (1).

The reference code is as follows:

 Public classReverseenglishsentence {Private Static Char[] A;  Public StaticString reversestring (Char[] SA,intFrom,intTo ) {         while(from<To ) {            Chart =Sa[from]; Sa[from++] =Sa[to]; Sa[to--] =T; }        returnstring.valueof (SA); }     Public Static voidleftrotatestring (String str) {if(str = =NULL|| Str.length () = = 0) {System.out.println ("It ' s wrong!"); }        Char[] sa =Str.tochararray (); intLen =sa.length; Char[] Saall = reversestring (sa,0,len-1). ToCharArray (); intj = 0; String a=NULL;  for(inti = 0;i<len;i++){            if(Saall[i] = = "| | i = len-1) {a= ReverseString (saall,j,i-1); J=i+1;    }} System.out.print (a); }         Public Static voidMain (string[] args) {//TODO auto-generated Method StubString str = "I am a student.";            Leftrotatestring (str); }}
View Code

1.1 Rotation of the string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.