"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) "