1、题目

58. Length of Last Word——Easy

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的更多相关文章

  1. <LeetCode OJ> 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  3. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. 58. Length of Last Word【leetcode】

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  5. 【一天一道LeetCode】#58. Length of Last Word

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

  6. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  7. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. 【LeetCode】58 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

随机推荐

  1. SpringBoot整合WEB开发--(三)文件上传

    文件上传: Java中文件上传一共涉及到两个组件,CommonsMultipartResolver和StandardServletMultipartResolver,其中CommonsMultipar ...

  2. 松软科技web教程:JavaScript HTML DOM 事件监听器

    addEventListener() 方法 实例 添加当用户点击按钮时触发的事件监听器: document.getElementById("myBtn").addEventList ...

  3. SpringBean的生命周期以及循环依赖过程

    上面就是springBean的大致生命周期. Bean的创建过程 创建Bean之前会调用Bean工厂的后置处理器,可以获取到BeanDefinition Bean的初始化过程 初始化之前会调用前置处理 ...

  4. Android开发实战——记账本(4)

    开发日志(4)——MainActivity 在MainActivity中编写了几个方法.首先,点击账本的一条记录可以选择删除他,然后重写了fab,使之在点击他后能够添加记录.还写了删除全部记录的方法. ...

  5. mysql 同时支持多少连接MYSQL 查看最大连接数和修改最大连接数

    MySQL查看最大连接数和修改最大连接数 1.查看最大连接数 show variables like '%max_connections%'; 2.修改最大连接数 set GLOBAL max_con ...

  6. Pycharm调试django项目时发现断点失效

    解决方法: 第一步: 第二步: 点击 Edit Configuration 第三步 : 点击 + 选择python 填写相关参数信息 或者 点击ok  完成配置  重启 IDE 注意   重启IDE ...

  7. Git的基本使用 -- 创建本地仓库

    下载安装 Git-2.25.0-64-bit .exe 查看是否安装成功 git --version 创建本地仓库 创建一个文件夹用于存放项目文件 在创建好的文件中右键选择 Git Bash Here ...

  8. 记一次使用正则表达式+foreach控制器调试

    使用forEach控制器时,变量为上一个请求返回的参数(通过正则表达式提取),设置好控制器的起始值后发现每次都是只执行一个,然后开始怀疑人生,百度了各种材料,最后还是决心好好的观察自己每一步是不是错了 ...

  9. 很多win10系统用户都遇见了开机发现任务管理器中有个系统中断进程占用cpu99%的问题,

    很多win10系统用户都遇见了开机发现任务管理器中有个系统中断进程占用cpu99%的问题,尝试了网上提供的方法都不能得到有效的解决.下面小编就为大家详细的介绍电脑工程师提供的正确的解决姿势. 出现系统 ...

  10. 查询 keystore文件的签名信息

    需要安装jdk 在安装 jdk的/bin文件夹下 keytool -v -list -keystore [keystore文件的路径]