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. String 类实现 以及>> <<流插入/流提取运算符重载

    简单版的String类,旨在说明>> <<重载 #include <iostream> //#include <cstring>//包含char*的字符 ...

  2. SQL数据库对象名无效的解决方法

    对象名 'dbo.xxxx' 无效. 最后找到如下方法解决:原因是必须把所有以前的所有者改为DBO就不会出问题了. 执行下面语句,更改所有表的所有者为DBO exec sp_msforeachtabl ...

  3. jQuery的init都做了些什么

    // 初始化jQuery对象,即jQuery.fn.init对象 // @param selector 选择器,可能是DOM对象.html字符串.jQuery对象.函数或其他任意值. // @para ...

  4. Mysql带返回值与不带返回值的2种存储过程

    过程1:带返回值: 1 drop procedure if exists proc_addNum; 2 create procedure proc_addNum (in x int,in y int, ...

  5. yii---生产链接的方法

    yii生成链接的方法: Yii::$app->urlManager->createUrl('xxx/xxx') <?= Yii::$app->urlManager->cr ...

  6. SHU 第15届上海大学程序设计联赛夏季赛[热身赛] 第三题(G题) - 英语成绩

    看完题目就觉得是个图论题…… 每个人的成绩就是vertice,两个人的分数差就是edge,那么肯定类似于一种relax的方式,不断将每个人的成绩的min往上提, 当然,单纯的遍历一遍G.E肯定不可能就 ...

  7. python 将字符串转换为字典

    在一般的工程处理中,需要将获取的字符串数据转换为字典,这样处理起来会非常方便. 我获取的是json数据: content = {"corpus_no":"64702772 ...

  8. linux:查看以及管理进程

    学习笔记内容概要 进程查看的命令:top,ps,pstree 进程管理的命令:kill,nice,renice 查看进程: 一.top工具 top 工具是我们常用的一个查看工具,能实时的查看我们系统的 ...

  9. flask 日志

    https://www.polarxiong.com/archives/Flask%E4%BD%BF%E7%94%A8%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95%E5%8 ...

  10. 02 - nginx - 反向代理、限速

    一.Nginx反向代理 代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器. 代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬盘中, ...