"152-reverse Words in a string (reverses the word in the string)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue"
,
Return "blue is sky the"
.
Main Topic
Given a string, invert it, and its word does not go
Thinking of solving problems
Reverses the entire string first, and then reverses each word
Code Implementation
Algorithm implementation class
Public class solution { PublicStringReversewords(String s) {if(s = =NULL) {return NULL; }Char[] chars = S.tochararray ();//character compression intReallength = Compresswhitespace (chars);//Swap entire stringSwaprange (chars,0, Reallength-1);//Record The beginning of a word intStart =0;//Record the end of a word intEnd while(Start < Reallength) {//Find the first non-whitespace character starting from the start position while(Start < Reallength && Chars[start] = ="') {start++; } end = Start +1;//Find the first blank character while(End < Reallength && Chars[end]! ="') {end++; }//Invert characterSwaprange (chars, start, end-1);//Record a new start positionstart = end; }return NewString (chars,0, reallength); }/** * Compress white space Characters of character array * * @param chars character array * @return New Length */ Public int Compresswhitespace(Char[] chars) {if(Chars = =NULL|| Chars.length = =0) {return 0; }//Place the character in the position intpos =0; for(inti =0; i < chars.length; i++) {//Start looking for the first non-whitespace character from the I position while(I < chars.length && chars[i] = ="') {i++; }//have finished processing if(I >= chars.length) { Break; }//Start processing non-whitespace characters from the I position until white space characters are encountered //is to deal with a single word while(I < chars.length && Chars[i]! ="') {Chars[pos] = chars[i]; pos++; i++; }//Finish one word to empty one, the last word does not have empty one "a" if(Pos < Chars.length) {Chars[pos] ="'; } pos++; }//int result = Pos-1;//System.out.println ("|" + New String (chars, 0, result) + "|"); //Description only white space characters in the string if(pos = =0) {return 0; }Else{//System.out.println ("|" + New String (chars, 0, pos-1) + "|"); //Minus one is to remove the extra space, see "a" returnPOS-1; } }/** * Reverses the character of the [x, Y] position in the word count array * * @param chars character array * @param x x position * @param /c6> y y position * / Public void Swaprange(Char[] chars,intXintY) { for(× < Y; x + +, y--) {Swap (chars, x, y); } }/** * Swap the word X, y two positions in the array * * @param chars character array * @param x x Position * @param Y y position * / Public void Swap(Char[] chars,intXintY) {Charz = chars[x]; CHARS[X] = Chars[y]; Chars[y] = Z; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47801753"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "152-reverse Words in a string (reverses the word in the string)"