题目来源:

  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. CF R303 div2 C. Woodcutters

    C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. yii_CGridView_ajax_pagination_and_ajax_sort

    本文主要内容: 1, 正常情况下 CGridView 实现 Ajax 分页和排序的原理 2, 分页和排序无法Ajax的情况分析 3, 自定义分页(重写CLinkPager)后如何实现 Ajax 分页和 ...

  3. poj 2228 Naptime dp

    这个题目的状态还是比较好想的,dp[i][j]表示已经睡了i个时段,最后睡在j时段的最优值,但是需要处理环的情况,我的做法是算两次,第一次不处理环,第二次强制性要求第一个时段需要睡,然后查看dp[m] ...

  4. 如何使用ssh-keygen生成key

    ssh-keygen - 生成.管理和转换认证密钥 通常使用:[b]ssh-keygen -i -f 公密匙名>> authorized_keys[/b] 语法详细介绍 [code]ssh ...

  5. linux下coredump的产生及调试方法

    什么是coredump 通常情况下coredmp包括了程序执行时的内存,寄存器状态,堆栈指针,内存管理信息等.能够理解为把程序工作的当前状态存储成一个文件.很多程序和操作系统出错时会自己主动生成一个c ...

  6. HDU2159:FATE(二维完全背包)

    Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级.现 ...

  7. python 命令行参数,以及文件操作

    #demo.py #!/usr/bin/python import sys print sys.argv #python demo.py 11 22 33 44 55 ['demo.py', '11' ...

  8. Linux下并发网络设计之I/O复用

    I/O 流: 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. 不管是文件,还是套接字,还是管道,我们都可以把他们看作流. 之后我们来讨论I/O的操作 ...

  9. Android乐学成语之自定义Adapter

    一.首先对Adapter概念深刻的了解 首先看看他的继承图

  10. MATLAB - 为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像?

    Q:为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像? A:数据类型如果是double,imshow的处理范围是0-1数据类型如果是uint8,imshow的处理范围是0 ...