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 ...
随机推荐
- 配置Mongodb
MS Windows: 常用命令: 查看帮助 mongod help/mongod -h 查看版本 mongod --version 启动/停止/重启服务 net start/stop/restart ...
- 数据库之一、数据库简介及SQL概要
1.数据库简介: 数据库(Database,DB)是一个长期存储在计算机内的.有组织的.有共享的.统一管理的数据集合.简单来讲就是可以放大量数据的地方.管理数据库的计算机系统称为数据库管理系统(Dat ...
- IDEA与Tomcat相关配置
idea会为每一个Tomcat部署的项目,独立建一份配置文件. 配置文件所在位置 怎么部署的(查看虚拟目录)使用的第三种部署方式 部署项目存放的路径 项目目录和Tomcat部署目录 Tomcat真正访 ...
- windows7安装 npm和cnpm
1.node.js下载地址: https://nodejs.org/en/download/ 我选择的安装路径是D:\files\nodejs 2.查看安装是否成功 3.创建文件夹 在D:\files ...
- selenium参数化-ddt模块
DDT介绍: DDT(数据驱动测试)允许您通过使用不同的测试数据运行一个测试用例来使其倍增,并使它显示为多个测试用例.要使用DDT需要安装,安装命令:pip install ddt 使用方法: dd. ...
- 【Markdown】新手快速入门基础教程
Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档.当前许多网站都广泛使用 Markdown 来撰写帮助文档或是用于论坛上发表消息.例如:GitHub.简书.reddi ...
- dbShape
Usage: dbShape [-help] [-d] [-step <step>] [-output {polygon rect hrect area}] <shapeList&g ...
- idea中使用Data Source and Drivers时,如果使用自己自定义的jar包
- dw选择器
选择器并没有一个固定的定义,在某种程度上说,jQuery的选择器和样式表中的选择器十分相似.选择器具有如下特点:1.简化代码的编写2.隐式迭代3.无须判断对象是否存在jQuery 的选择器可谓之强大无 ...
- buuctf misc 刷题记录
1.金三胖 将gif分离出来. 2.N种方法解决 一个exe文件,果然打不开,在kali里分析一下:file KEY.exe,ascii text,先txt再说,base64 图片. 3.大白 crc ...