Flip word order VS left spin string

Source: Internet
Author: User

All the content comes from the "sword Point offer".

Topic One:

Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters in the word does not change. For simplicity, punctuation is treated like normal characters. For example, the input string "I am a student.", the output

"Student. A am I ".

ANSWER:

voidReverseChar*pbegin,Char*pEnd) {    if(Pbegin = = NULL | | pEnd = =NULL)return;  while(Pbegin <pEnd) {        CharTMP = *Pbegin; *pbegin = *pEnd; *pend =tmp; Pbegin++; PEnd--; }}Char* Reversesentence (Char*pDate) {    if(PDate = =NULL)returnNULL; Char*pbegin =pDate; Char*pend =pDate;  while(*pend! =' /') PEnd++; PEnd--;    Reverse (Pbegin, pEnd); Pbegin= PEnd =pDate;  while(*pbegin! =' /')    {        if(*pbegin = =' ') {Pbegin++; PEnd++; }        Else if(*pend = =' '|| *pend = =' /') {reverse (Pbegin,--pEnd); Pbegin= ++pEnd; }        ElsepEnd++; }    returnpDate;}

Topic Two:

The left rotation of a string is the transfer of several characters in front of the string to the end of the string. Define a function that implements the function of a string left rotation operation. For example, the input string "ABCDEFG" and the number 2, the function will return the left rotation of 2 bits to get the result "cdefgab".

ANSWER:

Char* Leftrotatestring (Char*PSTR,intN) {    if(PStr! =NULL) {        intNlength = static_cast<int>(strlen (PSTR)); if(Nlength >0&& n >0&& N <nlength) {            Char*pfirststart =pStr; Char*pfirstend = Pfirststart + N-1; Char*psecondstart = Pfirststart +N; Char*psecondend = pStr + nlength-1;            Reverse (Pfirststart, pfirstend);            Reverse (Psecondstart, psecondend);        Reverse (Pfirststart, psecondend); }    }    returnpStr;}

There are also corresponding topics on the Leetcode.

https://leetcode.com/problems/reverse-words-in-a-string/

Flip word order VS left spin string

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.