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 ...
随机推荐
- python之路之课后作业
以下代码只包含管理员代码,用户代码和管理员相似 #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os sys.path. ...
- 图的dfs遍历模板(邻接表和邻接矩阵存储)
我们做算法题的目的是解决问题,完成任务,而不是创造算法,解题的过程是利用算法的过程而不是创造算法的过程,我们不能不能陷入这样的认识误区.而想要快速高效的利用算法解决算法题,积累算法模板就很重要,利用模 ...
- biquad filter实现
原始频谱: LPF: HPF: 代码: #include<stdio.h> #include<stdlib.h> #include<errno.h> #includ ...
- 每天进步一点点------MicroBlaze
有了前面两个实例的铺垫,下面这个工程就要带大家尝试搭建一个基于MicroBlaze的应用.特权同学也是第一次接插Xilinx的嵌入式开发平台,跑了一个流程下来,正如所料,和Alter ...
- Linux jpeglib库的安装
tar -zxvf jpegsrc.v9.tar.gz cd jpeg9 ./configure --enable-shared --enable-static 分别对动态链接库和静态链接库的支持 ...
- mysql忘记密码,更改密码
对MySQL有研究的读者,可能会发现MySQL更新很快,在安装方式上,MySQL提供了两种经典安装方式:解压式和一键式,虽然是两种安装方式,但我更提倡选择解压式安装,不仅快,还干净.在操作系统上,My ...
- execute command denied to user 'maintain'@'%' for routine
GRANT ALL PRIVILEGES ON *.* TO 'maintain'@'%' ; FLUSH PRIVILEGES;
- 解决VS2017中出现:This function or variable may be unsafe
解决办法:项目名称-右键属性-C/C++ - 预处理器 -预处理器定义 - 右侧下拉框中选择"编辑"- 在第一个编辑框中添加_CRT_SECURE_NO_WARNINGS
- C语言随笔5:函数、函数指针
函数 C语言中函数参数传递时,将实参的值拷贝到函数参数的存储区中.这种传递参数的方式称为按值传递. 函数不会访问实参本身,访问的是函数存储在栈区的副本,不会改变实参.函数凋用结束,函数在栈区的内容释放 ...
- jquery获取ul下的所有li个数
通过jquery获取ul下所有li的个数(eg) $("ul li").length 通过jquery设置标签css的样式(eg)$("#div").css({ ...