题目简述

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.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,

Given s = "Hello World",

return 5.

解题思路:

注意空格就好

class Solution:
# @param s, a string
# @return an integer
def lengthOfLastWord(self, s):
if len(s) == 0:
return 0
i = len(s) -1
while s[i] == ' ': if i == 0:
return 0
i -= 1
res = s[0:i+1].split(' ')
return len(res[-1])

【leetcode】Length of Last Word的更多相关文章

  1. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

  2. 【leetcode】length of last word (easy)

    题目: 输入字符串 s,返回其最后一个单词的长度 如 s="Hello World"   返回5 s="Hello World    "   返回5 s=&qu ...

  3. 【Leetcode】【Easy】Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. 【LeetCode】748. Shortest Completing Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】819. Most Common Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...

  6. 【leetcode】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  7. 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...

  8. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  9. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

随机推荐

  1. List接口方法使用(PS:Java 编程思想阅读小结)

    1.用代码说话 package JavaProject; import java.util.*; public class A{ public static void main(String[]arg ...

  2. errored out in DoExecute, couldn't PrepareToExecuteJITExpression

    error: Couldn't materialize struct: size of variable <varName> disagrees with the ValueObject' ...

  3. RabbitMQ学习系列(五): RPC 远程过程调用

    前面讲过一些RabbitMQ的安装和用法,也说了说RabbitMQ在一般的业务场景下如何使用.不知道的可以看我前面的博客,http://www.cnblogs.com/zhangweizhong/ca ...

  4. 深入理解Java中的String

    一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...

  5. 对路径的访问被拒绝,解决之后又报-未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。

    服务器环境:Server 2008  64位系统 问题:在导入Excel题录表时报错,1对路径的访问被拒绝,2未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序. 解决方案 ...

  6. zend studio面板功能

    不小心把zend界面右边的显示类中各个方法的窗口关掉了,如何打开呢: 法一:点击Windows菜单,选择show view项,选择outline即可: 法二:点击Windows菜单,选择Reset P ...

  7. Xamarin.Forms 简介

    An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...

  8. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  9. iOS-关于使用其他应用打开本应用文档

    简介:本片文章是对官方文档的翻译,非常的感谢文章的翻译者:颐和园 官方地址:Document Interaction Programming Topics for iOS 文章的介绍内容: ***** ...

  10. sqlservcer行列互转

    普通行列转换 行转列 假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/---- ...