Given an input string, reverse the string word by word. For Example,given s = "The Sky is Blue" and return "Blue is Sky the". Update (2015-02-12): For C programmers:try to solve it in-place in O (1) space.
Test instructions: Reverse the word in the string, pay attention to the processing of the space!!!
Public class solution { public string reversewords (String s) { int length=s.length (); // char[] ch=s.tochararray (); if (s==null | | length==0) return s; int end=length; stringbuilder ret=new stringbuilder (); for (int i=length-1;i>=0;i--) { if (S.charat (i) = = ' ') end=i; else if (i==0 | | s.charat (i-1) = = ' ') { // If there is a word in it, add a space before adding a new word &nbSp; if (Ret.length ()!=0) ret.append (' '); ret.append ( S.substring (i,end)); } } return ret.tostring ( ); }}
PS: The idea of the great God in the group. It's very concise. Traverse from backward to forward. Find the word and add it to the RET,
Leetcode 448. Find all Numbers disappeared in an Array Java language 151. Reverse Words in a String