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
- Let's start with a flip of the whole sentence, and then flip each word.
- 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