[LeetCode]题解(python):058-Length of Last Word
题目来源:
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的更多相关文章
- 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 ...
- <LeetCode OJ> 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode(59)Length of Last Word
题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...
- 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 ...
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- [leetcode]Length of Last Word @ Python
原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...
- 【LeetCode】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
随机推荐
- css vertical-align全解
CSS 的属性 vertical-align 指定了内联(inline)元素或表格单元格(table-cell)元素的垂直对齐方式. 要记住:vertical-align不影响块级元素中内容的对齐. ...
- start stack
Start OpenStack Services After launching your stack by Devstack, you maybe stop some services or reb ...
- JAVA异常设计原则
异常是面向对象语言非常重要的一个特性,良好的异常设计对程序的可扩展性.可维护性.健壮性都起到至关重要. JAVA根据用处的不同,定义了两类异常 * Checked Exception: Exc ...
- Javascript之Dom学习
1.简介:DOM是一套对文档的内容进行抽象和概念化的方法.在现实世界里,人们对"世界对象模型"一定不会陌生,当人们用"房子","汽车"等这一 ...
- c++ 深浅拷贝
对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a;而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象拷贝的简单例子. #in ...
- KMP算法番外篇--求解next数组
KMP算法实现字符串的模式匹配的时间复杂度比朴素的模式匹配好很多,但是它时间效率的提高是有前提的,那就是:模式串的重复率很高,不然它的效率也不会凸显出来.在实际的应用中,KMP算法不算是使用率很高的一 ...
- Oracle 批量造数据
1.通过存储过程方式 CREATE OR REPLACE procedure XXZY.p_test is i number; begin .. loop INSERT INTO test_job V ...
- Centos 安装docker报错
错误信息: 安装报错:Transaction check error: file /usr/lib/systemd/system/blk-availability.service from inst ...
- Linux第三方源
由于版权等各方面原因,很多时候在基础安装完Linux后,满多软件并不包含在yum(对于Ubuntu可能是apt-get)源中. 因此可以去下载第三方源,安装下载第三方软件.当然,如果习惯了源代码编译安 ...
- 盛希泰:办公室就像男人的春药——人的一生的精力是有限的,你把有限的时间分配给谁决定你的成败——你有N多选择,你人生的积累就是N多选择加起来的结果
欢迎关注“创事记”的微信订阅号:sinachuangshiji 创事记注:12月22日晚上,盛希泰在清华大学旧经管报告厅面对清华师生讲了一堂<创业引导课>.本文由洪泰帮根据课堂录音整理完成 ...