"Leetcode-Interview algorithm classic-java Implementation" "" 058-length of Last word (length of final word) "

Source: Internet
Author: User

"058-length of last word (length of final word)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Given A string s consists of Upper/lower-case alphabets and empty space characters ‘ ‘ , return the length of last word I n the string.
If the last word does not exist, return 0.
Note:a word is defined as A character sequence consists for non-space characters only.
For example,
Given s = "Hello World" ,
Return 5 .

Main Topic

Returns the last word length in a string, given a string of uppercase and lowercase alphabetic groups and spaces.

Thinking of solving problems

First the first letter from the position x, if not found on the return 0, if found, and then find the first space of the bit is recorded as Y (y may be-1, because no space is found), return the result X-Y.

Code Implementation

Algorithm implementation class

 Public  class solution {     Public intLengthoflastword (String s) {int Index= S.length ()-1;//From behind to find the first not "character         while(Index>=0&& S.charat (Index) =="') {Index--; }if(Index<0) {return 0; }intTMP =Index;//Execute to the following note that there is a last word        //From the back forward to find the first one is "the character         while(Index>=0&& S.charat (Index) !="') {Index--; }returnTMP-Index; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47164433"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "" 058-length of Last word (length of final word) "

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.