[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 ...
随机推荐
- 浅谈 android-query
介绍:android-query他是在GitHub上的一个开源轻量级的封装库,它集成了网络 .图片加载等模块,可以应用在android中的一些异步应用以及UI的操纵上,通过使用这个框架,使androi ...
- Jsoup代码解读之四-parser
Jsoup代码解读之四-parser 作为Java世界最好的HTML 解析库,Jsoup的parser实现非常具有代表性.这部分也是Jsoup最复杂的部分,需要一些数据结构.状态机乃至编译器的知识.好 ...
- 学习pthreads,给线程传递多个參数
上篇博文中.boss线程给其它线程传递的仅仅有一个參数,那么假如是多个參数呢?怎么传递呢?也许你会有这种疑问,带着这个疑问,我们进入本文的世界,这里传递多个參数,採用结构体,为什么呢?由于结构体里能够 ...
- 线性规划问题的matlab求解
函数:[x, fval] = linprog(f, A, b, Aeq, Beq, LB, UB) 返回的x:是一个向量——在取得目标函数最小时各个xi的取值: 返回的fval:目标函数的最小值: 参 ...
- 联想S720/S720i通刷刷机包 Vibe V1.0
ROM介绍 基于官方最新S116底包制作,保证足够的稳定性. 增加VIBE元素,看起来更加大气.美观. 首次增加VIBE元素,720i执行起来无压力,720可能会有点卡.自行酌情刷入. 有bug请文明 ...
- Mybaits入门之起航
前言 Mybaits技术现在很多公司都在使用,它提供了简单的API可以快速进行数据库操作,所以不管是自己做系统还是找工作都有必要了解一下. 学习一门技术如果是入门的话要么买书要么就是阅读官方的文档,而 ...
- 搭建Myeclipse下Java Web开发环境
1.准备 先下载软件:Myeclipse:http://www.xiazaiba.com/html/23858.html tomcat:http://files.cnblogs.com/files/l ...
- UltraISO做U盘启动盘教程
用UltraISO做U盘启动盘教程 注意:制作前请先备份u盘内重要文件 vista.win7系统需要以管理员身份运行UltraISO,其他系统直接运行即可 1.打开UltraISO选择iso镜像文件 ...
- c++ primer plus 习题答案(8)
p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...
- servlet三种实现方式之一实现servlet接口
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第一种示例代码如下(已去掉包名): import ...