Character string Movement (if the character string is a combination of * and 26 letters, move * to the leftmost, move the letters to the rightmost, and keep the relative order unchanged ), minimum Time and space complexity

Source: Internet
Author: User

Character string Movement (if the character string is a combination of * and 26 letters, move * to the leftmost, move the letters to the rightmost, and keep the relative order unchanged ), minimum Time and space complexity

If there is no requirement to ensure that the relative sequence of letters remains unchanged, it is the easiest to move the exchange element to the center using two double-ended pointers. However, this movement has a jump, so the relative order cannot be guaranteed.

For the method, see the notes:

The main idea is to use a point pointer to point to the first * at the end, and then use the pointer let to point to the first letter before, in turn, the letter and * sequence at the end of let and point are exchanged. Then find the next letter and * sequence until a pointer points to the first address of the string.

To scan from the back to the back, you need to swap the numbers and letters you encounter, so that you can ensure that the subsequent letters are still behind.

# Include "stdafx. H "# include <iostream> using namespace STD; void switcher (char * Str) {int Len = strlen (STR); char * Let = STR + Len; char * point = STR + Len; while (point! = STR & let! = Str) // cyclic termination condition {While (* point! = '*' & Point! = Str) // find the first * {point --;} If (point = Str) from the back and forth // if the first address of the string is found, that indicates that the entire string is * return; let = point; while (* Let = '*' & let! = Str) // start from the first * and find the first letter {Let --;} while (* let! = '*' & * Point = '*' & let! = Str) // exchange letters and * {// cout <* Let <"<-->" <* point <Endl; char CH = * Let; * Let = * point; * point = CH; let --; point -- ;}} int main () {char a [] = "*** AB ** CD *** e ** DF ** Ghi"; cout <A <Endl; int Len = sizeof () /sizeof (A [0]); switcher (a); cout <Endl; getchar (); Return 0 ;}

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.