美文网首页
leetcode 58--string

leetcode 58--string

作者: Ariana不会哭 | 来源:发表于2018-12-14 05:42 被阅读0次

这个题对于Java相当简单:直接用split对给定的string进行分段。不要忘记边界处理:


图片.png
class Solution {
    public int lengthOfLastWord(String s) {
        if(s.length()<=0)
            return 0;
        String [] temp=s.split(" ");
        if(temp.length==0)
            return 0;
        return temp[temp.length-1].length();
    }
}

相关文章

网友评论

      本文标题:leetcode 58--string

      本文链接:https://www.haomeiwen.com/subject/lezihqtx.html