leetcode——58. 最后一个单词的长度

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        if s==' '*len(s):
            return 0
        if ' ' not in s:
            return len(s)
        a=s.split(' ')
        i=-1
        while a[i]=='':
            i-=1
        return len(a[i])
执行用时 :44 ms, 在所有 python3 提交中击败了78.73%的用户
内存消耗 :13.6 MB, 在所有 python3 提交中击败了5.01%的用户
 
                                               ——2019.10.16
 

简单题
public int lengthOfLastWord(String s) {
        s = s.trim();
        String[] str = s.split(" ");
        int len = str.length;
        if(len == 0){
            return 0;
        }
        return str[len-1].length();
    }

 

 

——2020.7.8

posted @ 2019-10-16 21:17  欣姐姐  阅读(164)  评论(0编辑  收藏  举报