I. Description of the topic
Given an input string, reverse the string word by word.
For example,
Given s = " the sky is blue ",
Return " blue is sky the ".
Second, analysis
Note the following: 1. When there are spaces in the head or tail of a string, the end is eliminated
2, when the number of spaces between two substrings is greater than 1 o'clock, just keep a
Problem-Solving ideas: 1, first, the entire string is reversed
2. Then, use the Split function to cut the string and get each string reversed
3. Finally, in the resulting string the header and trailing spaces are removed using the trim () function
Third, the code implementation
public class Solution {public String reverse (string s) { //This is a reversal of a string, which is added to StringBuffer from the end of the head to the IF (S.length () <=1 ) {return s;} StringBuffer SB =new StringBuffer (); for (int i=s.length () -1;i>=0;i--) {sb.append (S.charat (i));} return sb.tostring ();} /* * Split according to the spaces in the string, invert each substring and add a space after it */public string reversewords (string s) { s=reverse (s); String[] Str=s.split (""); Split StringBuffer SB =new stringbuffer (); for (int i=0;i<str.length;i++) {if (!str[i].equals (")) {Sb.append ( Reverse (Str[i]). Append (""); Invert the string and add it to StringBuffer}}return sb.tostring (). Trim (); public static void Main (string[] args) {String s = ""; Solution st= New Solution (); String Str=st.reversewords (s); System.out.println (str);}}
Iv. if you want to preserve all whitespace in a string
public class Solution {public String reverse (string s) {//This is a reversal of a string, which is added to StringBuffer from the end of the head to the IF (S.length () & Lt;=1) {return s;} StringBuffer SB =new StringBuffer (); for (int i=s.length () -1;i>=0;i--) {sb.append (S.charat (i));} return sb.tostring ();} /* * Split according to the spaces in the string, invert each substring and add a space after it */public string reversewords (string s) {s=reverse (s); String[] Str=s.split ("");//system.out.println (str.length); StringBuffer SB =new stringbuffer (); if (str.length!=0) {for (int i=0;i<str.length-1;i++) {//if (!str[i].equals ("")) { Used to determine whether the current is not empty, if it is, add a space, if not, reverse the string//str[i]=reverse (Str[i]), Sb.append (reverse (str[i)). Append (""); Else{sb.append ("");}} Sb.append (reverse (str[str.length-1)); Reverses the last string and adds it to StringBuffer}//system.out.println (S.length ());//system.out.println (Sb.length ()); Sb.length ()!=s.length ()) {/* * Because the string is cut, the later space is discarded, (string = "Zhao Yan", when the string is cut, the space after the Yan is ignored) * So when the resulting character String length is not equal to the original string length, */int len=s.length ()-sb.length () at the back of the space. j=0;j<len;j++) {sb.append ("");}} System.out.println (S.length ());//system.out.println (Sb.length ()); return sb.tostring (); } public static void Main (string[] args) {String s = ""; Solution st= New Solution (); String Str=st.reversewords (s); System.out.println (str);}}
"Leetcode" Reverse Words in a String Java implementation