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.

Example:

Input: "Hello World"
Output: 5

就先利用s.strip()将前后的space去掉, 然后再用split(' ') 将最后的word的length得到并返回.

Code

class Solution:
def lengthOfLastWord(self, s):
s = s.strip()
if not s: return 0
return len(s.split(' ')[-1])

[LeetCode] 58. Length of Last Word_Easy tag: String的更多相关文章

  1. LeetCode 58. Length of Last Word

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

  2. [LeetCode] 58. Length of Last Word 求末尾单词的长度

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

  3. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  4. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  5. [leetcode]58. Length of Last Word最后一个词的长度

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

  6. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  7. Leetcode 58 Length of Last Word 难度:0

    https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...

  8. Leetcode 58 Length of Last Word 字符串

    找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...

  9. [LeetCode] 415. Add Strings_Easy tag: String

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...

随机推荐

  1. Android应用的自动升级、更新模块的实现(转)

    我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新.得益于Android系统的软件包管理和安装机制,这一功能实现起来相当简单,下面我们就来实践一下.首先给出界面效果: ...

  2. Qt编写的RTSP播放器+视频监控(vlc版本)

    几天写了个ffmpeg版本,今天特意抽空改写个vlc版本,之前vlc播放视频后,被接管了不能识别到鼠标,需要重新编译vlc源码得到支持鼠标消息的版本. /*** vlc视频播放类 作者:feiyang ...

  3. 备忘,commons-codec中可能用到的一些加密字符串的方法

    commons-codec中提供了一些加密解密字符串的方法,我们可以直接使用 1.MD5加密: String source = "source"; DigestUtils.md5H ...

  4. RSA加密工具类(非对称加密算法)

    import com.jfinal.log.Log;import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; ...

  5. centos记录uptime,tomcat日志切割,远程拷贝日志脚本

    1.uptime日志脚本(每天记录) #!/bin/sh dir=/tmp/uptime_log process=`ps -ef|grep $|grep -v "grep" |gr ...

  6. linux 命令行常用快捷键

    linux命令行常用快捷键,区别于vim编辑器快捷键.熟练掌握下面的快捷键可提高操作linux的工作效率.当然最重要的是可以装屌. 1.移动光标快捷键Ctrl+a光标回到命令行首* Ctrl+e光标回 ...

  7. 每日定时收集MySQL日志并发送邮件

    本次脚本系统版本 MySQL日常运行产生的慢查询日志收集并且发送邮件 vim mysql-slow-log.sh # 这是一个收集慢查询的脚本,每天收集MySQL慢查询日志并且发送邮件#!/bin/b ...

  8. 背包----Proud merchants

    一件物品p,q,v 分别表示物品的价格,钱少于q时就不能买,和物品的价值 n表示物品数量,m表示初始有的钱 问最多能买多少东西 01背包 注意点是,因为有一个q作为限制条件,所以n件物品不是随意选的了 ...

  9. LightOJ 1030 - Discovering Gold - [概率DP]

    题目链接:https://cn.vjudge.net/problem/LightOJ-1030 You are in a cave, a long cave! The cave can be repr ...

  10. Chainer的初步学习

    人们都说Chainer是一块非常灵活you要用的框架,今天接着项目里面的应用,初步接触一下,涨涨姿势,直接上源码吧,看着好理解.其实跟Tensorflow等其他框架都是一个套路,个人感觉更简洁了. & ...