题目来源:

  https://leetcode.com/problems/length-of-last-word/


题意分析:

  给出只包括大小写和空格的字符,输出最后一个单词的长度。


题目思路:

  从最后一个字符开始搜索,如果字符非空格,则往前推一位,直到不是空格,此时记录起始位置。然后继续搜索,直到遇到下一个空格或者到了第一个位置,记为终点位置。长度则为起始位置减去终止位置。


代码(python):

  

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
size = len(s)
if size == 0:
return 0
i = -1;first = True;begin = -1
for j in range(size):
if s[i] != ' ':
i -= 1;first = False
elif first:
i-= 1;begin = i
else:
break
return begin - i

转载请注明出处:http://www.cnblogs.com/chruny/p/4988176.html

[LeetCode]题解(python):058-Length of Last Word的更多相关文章

  1. Java for LeetCode 058 Length of Last Word

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

  2. <LeetCode OJ> 58. Length of Last Word

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

  3. [leetcode.com]算法题目 - Length of Last Word

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

  4. LeetCode(59)Length of Last Word

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

  5. LeetCode练题——58. Length of Last Word

    1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...

  6. 058 Length of Last Word 最后一个单词的长度

    给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...

  7. [leetcode]Length of Last Word @ Python

    原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...

  8. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  9. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

  10. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

随机推荐

  1. Vim 实用技术,第 2 部分: 常用插件(转)

    http://blog.jobbole.com/20619/ 2.1. gzip(压缩文件支持) 作者:Bram Moolenar 网站脚本编号:无(包含在 Vim 的标准发布之中) 安装说明:无 功 ...

  2. Oracle的TPCC测试,原来也是个作弊的东西...

    http://www.oaktable.net/content/sorted-hash-clusters-rip 根据Jonathan Lewis老先生的测试实例,发觉cluster 的sort功能, ...

  3. clistctrl 虚拟列表

    一.什么是虚拟列表控件 虚拟列表控件是指带有LVS_OWNERDATA风格的列表控件.. 二.为什么使用虚拟列表控件 我们知道,通常使用列表控件CListCtrl,需要调用InsertItem把要显示 ...

  4. 判断变量是否存在(python)

    var = 1 # print True print 'var' in dir() # print False print 'va' in dir() # work for list , dict t ...

  5. js兼容性大全

    js有个第二定律好的属性/选择器一定不兼容/* 获取类名通用代码*/function getClassName(){ if(document.getElementsByClassName){ doso ...

  6. 设置cell背景色半透明

    cell.backgroundColor = [UIColor colorWithRed:(247.0/255.0) green:(151.0/255.0) blue:(121.0/255.0) al ...

  7. tomcat 部署web项目异常

    项目部署到Tomcat报这样的异常:validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending cla ...

  8. 如何修改MFC发布程序的图标

    (1)第一种方法,直接替换工程路径下面res下面的ico图标,然后重新编译,一般需要重启系统才会生效: (2)第二种方法,在VS工程资源预览窗口的ICO下增加一个ico资源,名为IDR_ICON1,然 ...

  9. C++时间获取

    http://net.pku.edu.cn/~yhf/linux_c/function/04.html   asctime(将时间和日期以字符串格式表示) 相关函数 time,ctime,gmtime ...

  10. 5.7.1.2 eval() 方法

    现在我们介绍最后一个方法,这大概是ECMAScript语言中最强大的一个方法:eval().eval()方法就想一个完整的ECMAScript解析器,它只接受一个参数,即要执行的ECMAScript( ...