[Interview algorithm Redo] reverse the order of words in a sentence

Source: Internet
Author: User

In the words of the work of the algorithm is really how??????

Although the work is not used, but you have to change work, can't resist the written interview to ask you some of this stuff.

Moreover, thinking more can help the mind to be active. The children's shoes can also be active and prevent rusting in the deep plunge into the project.

Words do not say much, the topic is as follows:

Title: Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters within the word is unchanged. Words are separated by spaces in sentences. For simplicity, punctuation is treated like ordinary letters.

For example, enter "I am a student.", then output "student." A am I ".

In Huang's diary, the Analysis method is

Reverses all the characters in the sentence first. In this case, not only the order of the words in the sentence is flipped, but also the characters in the word are flipped. We invert the characters within each word. Because the characters within the word are flipped two times, the order remains the same as the order in which they were entered.

Take the above input as an example. Flip "I am a student." All the characters in the ". Tneduts a Ma I", and then flip the order of the characters in each word to get "students." A AM I "is the output that meets the requirements.

That's a lot of trouble. There are wood, we can change a way of thinking, with stacks and containers to achieve:

Stack implementation ideas:

The loop statement looks for spaces, finds the words separated by spaces, and then leaves the stack on the line.

1 voidReverse (Const string&inStr) {2stack<string>Strstack;3     stringtemp;4      for(inti =0; I < instr.length (); ++i) {5         if(!isspace (Instr[i])) {6Temp + =Instr[i];7}Else {8 Strstack.push (temp);9temp ="";Ten         } One     } A     //here, the last value is pressed in . - Strstack.push (temp); -    //The variable must be declared here, because you are in the Operation Stack, size is changed, be sure to note the     intStackSize =strstack.size (); -      for(inti =0; i < stackSize; i++) { -cout << strstack.top () <<" " - Strstack.pop (); +     } -}

Container implementation ideas:

Similar to the stack, first find a space-delimited word, then put it into the container, and then reverse output on the line

1 voidReverse (Const string&s)2 { 3     stringtemp;4vector<string>result;5      for(inti =0; I < s.length (); ++i)6     { 7         if(Isspace (S[i]))8         { 9 Result.push_back (temp);Tentemp ="";  One         }  A         Else  -Temp + =S[i]; -     }  the Result.push_back (temp); -      for(intj = result.size ()-1; J! =-1; --j) -cout<<result[j]<<" ";  -cout<<Endl; +}

So it's done.

[Interview algorithm Redo] reverse the order of words in a sentence

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.