Leetcode 448. Find all Numbers disappeared in an Array Java language 151. Reverse Words in a String

Source: Internet
Author: User

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

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.