Java for Leetcode 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 ",
Return " blue is sky the ".

Problem Solving Ideas:

A lot of methods, the simplest way to directly press "" Spilt can, Java implementation is as follows:

    Public String reversewords (string s) {if (s = = NULL | | s.length () = = 0) return s; string[] str = s.split (""); StringBuilder sb = new StringBuilder (); for (int i = str.length-1; I >= 0; i--) if (Str[i].length () >= 1) sb.append (s Tr[i] + ""); if (Sb.length () > 1) Sb.deletecharat (Sb.length ()-1); return sb.tostring ();    }

Java for Leetcode 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.