"Leetcode" Reverse Words in a String Java implementation

Source: Internet
Author: User

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

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.