LeetCode练题——58. Length of Last Word
1、题目
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
Example:
Input: "Hello World"
Output: 5
2、我的解法
先采用strip方法
# -*- coding: utf-8 -*-
# @Time : 2020/2/7 20:41
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 58. Length of Last Word.py class Solution:
def lengthOfLastWord(self, s: str) -> int:
s1=s.strip()
lens = len(s1)
if lens > 0:
list1 = []
for i in range(lens):
if s1[i] == " ":
list1.append(i)
if len(list1) > 0:
a = list1.pop()
return lens - 1 - a
else:
return lens
else:
return 0
print(Solution().lengthOfLastWord("a "))
LeetCode练题——58. Length of Last Word的更多相关文章
- <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❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
- 【LeetCode】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
随机推荐
- mysql 原有的主键情况下设置自增字段
mysql 的自增字段只能是主键,如果原表已经有主键,需要设置自增字段应该怎么做呢? 1.alter table bu_staff drop primary key; 先删除表的主键 id为原表 ...
- c数据结构 -- 栈与队列
栈和队列 ·栈和队列是两种常用的.重要的数据结构 ·栈和队列是限定插入和删除只能在表的“端点”进行的线性表 栈 只能在队尾插入,只能在队尾删除 -- 后进后出 表尾称为栈顶:表头称为栈底 插入元素到栈 ...
- 经常犯的错误之递归写不全return
在写递归函数的时候,只在最后一层写return,中间的过程没有return,导致结果的丢失. 举个例子 LL query(LL i, LL k) { if (sum[i] < k) { ; } ...
- 整合SSM2
SSM框架的搭建 注意: 1.SpringMVC和Spring 不需要什么特殊的配置就可以结合 2.MyBatis和Spring 1)需要引入额外的jar包:m ...
- Makefile中的wildcard/notdir/patsubst
在Makefile规则中,通配符会被自动展开. 但在变量的定义和函数引用时,通配符将失效.这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的用法是:$(wildcard PATTE ...
- hdu 1532 Drainage Ditches(网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少 ...
- Python 数据分析实战 | 用数据带你回顾乔丹的职业生涯
乔丹是联盟上下公认的历史第一人,芝加哥公牛在他带领下几乎统治了上世纪 90 年代 NBA 整整 10 年,包括分别在 91-93 赛季和 96-98 赛季拿下的两次三连冠,要知道,NBA72 年历史上 ...
- POJ 3991 括号匹配问题(贪心)
I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple prob ...
- 计算几何-Ang-Rad-Vector
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 旋转,跳跃,梦境 ...
- buuctf 二维码
首先下载文件 然后用解压工具解压之后 发现是一个二维码 扫描二维码 并没有拿到 flag 然后将图片拖进 hxd中搜索PK发现有一个压缩包 将压缩包提取出来 暴力破解 然后得到密码 然后解压 然后得 ...