题目简述

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. ThinkPHP框架之验证码

    Think\Verify类可以支持验证码的生成和验证功能. 下面是最简单的方式生成验证码: $Verify = new \Think\Verify(); $Verify->entry(); 上面 ...

  2. MySQL安装与设置

    下载zip,配置 1,系统变量添加:...\mysql-5.7.10-winx64,环境变量添加:%MYSQL_HOME%\bin 2,修改MySQL.ini basedir=(同系统变量路径) da ...

  3. PHP常用函数整理

    推荐网址:http://php.net/manual/zh/http://www.w3cschool.cc/php/php-ref-array.html 错误报告: error_reporting(E ...

  4. PHP中instanceof关键字

    instanceof关键字是PHP5新增的新成员.在一些情况下,我们希望确定某个类属于哪种特定的类型.或者是否实现了特定的接口,这时我们就可以使用这个关键字确定一个对象是否是某个特定的类型,是否是从某 ...

  5. Could not find or load main class org.gradle.wrapper.GradleWrapperMain解决办法

    解决办法: gradlew is the gradle wrapper executable - batch script on windows and shell script elsewhere. ...

  6. Hibernate简易原生DAO的实现

    写在最前: 初学Hibernate,在尝试把JDBC项目移植到Hibernate的过程中,碰到了不少的麻烦,最让人心烦意乱的自然是SQL语句改动造成的代码混乱.其实不难,网上的解决方案有很多, 不过. ...

  7. [原创]Matlab2016b打包为C++的lib文件

    这几天在研究如何将Matlab的程序导入到C++进行调用. 由于需要使用到不少Matlab函数,所以之前就有些担心这些函数在导出后是否能够继续使用.不过之后觉得既然已经导出成了一个单独文件,相关运算应 ...

  8. Gedit 解决中文显示乱码问题

    详细请参考:http://wiki.ubuntu.org.cn/Gedit%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81 具体原因是Gedit的默认编码设置没有添加中文编码所 ...

  9. 计算机维修之操作系统的安装(windows、Mac、Linux)

    从大学开始接触了更多的计算机知识,我就在开荒一样,慢慢的接触多了,就想着安装操作系统一定要学会. 前前后后呢,我折腾过很多操作系统,在我的笔记本上存在过Windows.黑苹果.Linux.安卓等操作系 ...

  10. 解决svn问题:Wrong committed revision number: -1

    参考:http://my.oschina.net/luckyi/blog/291007