Flip word order column-Sword Point offer

Source: Internet
Author: User

Flip word order column title description

New Ket recently arrived with a new employee, fish, who always takes an English magazine in the morning and writes some sentences in the book. Colleague Cat was interested in what Fish wrote, one day he borrowed it from fish, but couldn't read it. For example, "student. A am I ". Later realized that this guy turned the sentence in the order of words, the correct sentence should be "I am a student." Cat Flip One by one the order of these words is not good, can you help him?

Ideas
    1. Let's start with a flip of the whole sentence, and then flip each word.
    2. Note: Each time a loop is flipped to flip a word between two spaces, and the condition when the last word is flipped
Code
PublicClassSolution {PublicStringReversesentence (String str) {if (str = = null) {return null;}if (str.length () = =1) {return str;} char[] Strchar = Str.tochararray ();Reverse (Strchar,0, Strchar.length-1); intleft =-1;intright =0;while (Right < Strchar.length) {if (strchar[Right] = = ") {Reverse (Strchar,Left +1,Right-1);left =Right;}right++;}Reverse (Strchar,Left +1,Right-1); return string.valueof (Strchar);} public void reverse (char[] data, int left, int right) {if (data = = NULL | | left >= right) {return;} while (left < right) {Char Temp = Data[left];d ata[left] = Data[ Right];d ata[right] = Temp; left++; right--;} return;}             

Flip word order column-Sword point offer

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.