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. BeautifulSoup的基本使用

    一.将一段文档传入BeautifulSoup的构造方法,得到一个文档的对象: from bs4 import BeautifulSoup Soup = BeautifulSoup(html_doc) ...

  2. mysql获取字段信息

    SELECT TABLE_SCHEMA AS `databaseName`, TABLE_NAME AS `tableName`, COLUMN_NAME AS `columnName`, DATA_ ...

  3. Git的基本使用 -- 分支管理

    查看分支 git branch 前面带 * 的为当前所在分支 创建分支 git branch 分支名 切换分支 git checkout 分支名 创建并切换到此分支 git checkout -b 分 ...

  4. 第三十六篇 入门机器学习——Jupyter Notebook中的魔法命令

        No.1.魔法命令的基本形式是:%命令   No.2.运行脚本文件的命令:%run %run 脚本文件的地址 %run C:\Users\Jie\Desktop\hello.py # 脚本一旦 ...

  5. Flink流处理(三)- 数据流操作

    3. 数据流操作 流处理引擎一般会提供一组内置的操作,用于对流做消费.转换,以及输出.接下来我们介绍一下最常见的流操作. 操作分为无状态的(stateless)与有状态的(stateful).无状态的 ...

  6. 题解【洛谷P1407】 [国家集训队]稳定婚姻

    题面 题解 很好的\(Tarjan\)练习题. 主要讲一下如何建图. 先用\(STL \ map\)把每个人的名字映射成数字. 输入第\(i\)对夫妻时把女性映射成\(i\),把男性映射成\(i+n\ ...

  7. scrapy 框架基本使用

    scrapy简介: Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 ...

  8. Swagger-ui接口文档

    参考地址 https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X#quick-annotation-overview   ...

  9. bugku web4

    打开打开,刚刚有个sb问我借lol号玩,浪费时间 继续干正事 随便输入后,提示     再好好看看... 出题人语文肯定不好 ,,,应该是这个 ‘再’ 吧 那我们查看源码 将那么明显的两行   进行 ...

  10. python 第三方库安装

    1.首先安装pip 2.在cmd中找到pip的安装路径,(一般在python的scripts文件中) 3.pip install 第三方库名称