[LeetCode]58. Length of Last Word

来源:互联网 发布:用js怎么写九九乘法表 编辑:程序博客网 时间:2024/06/02 02:28

题目描述: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 int lengthOfLastWord(String s) {        if(s==null||s.length()==0)return 0;        s = s.trim();        String[] split = s.split("\\s");        return split[split.length-1].length();    }
原创粉丝点击