58. Length of Last Word

来源:互联网 发布:手机淘宝的id号在哪里 编辑:程序博客网 时间:2024/04/29 03:47

Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

public class Solution {    public int lengthOfLastWord(String s) {        String [] sp = s.split(" ");              //for(int i=sp.length;i>=0;i--){              for(int i=sp.length-1;i>=0;i--){                  if(sp[i].length()>0)                      return sp[i].length();              }         return 0;      }}
0 0
原创粉丝点击