LeedCode 之 Length of Last Word

xiaoxiao2021-02-28  71

题目链接:Length of Last Word 这道题就相当的简单的,主要是对特殊字符串的处理,而且题目中说“If the last word does not exist, return 0.”我就一直以为当最后一个字符是“ ”,就表示要return0,实际不是这样的。其需要返回的是最后一个单词的长度,除非整个字符串中没有一个单词。 直接上代码就行:

public class Solution { public int lengthOfLastWord(String s) { int len = s.length(); if(len==0) return 0;//首先处理字符串为空的时候 String[] word; word = s.split(" "); int leng = word.length; if(leng==0) return 0;//在这里需要处理字符串中没有单词的情况下 int re = word[leng-1].length(); return re; } }
转载请注明原文地址: https://www.6miu.com/read-51242.html

最新回复(0)