作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/length-of-last-word/description/

题目描述

Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Examle:

input:

"Hello World"
"Hello World "
"Hello W orld"
"Hello Wo rld"

output:

5
5
4
3

题目大意

计算一个字符串中,最后一个不为空的单词的长度。

解题方法

库函数

使用库函数,方法比较简单,一行代码。

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
return len(s.strip().split(' ')[-1])

双指针

使用两个指针,一个指向最后一个字符串的结尾,一个指向最后一个字符串的开头。

Python代码如下:

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
N = len(s)
left, right = 0, N - 1
while right >= 0 and s[right] == " ":
right -= 1
left = right
while left >= 0 and s[left] != " ":
left -= 1
return right - left

C++代码如下:

class Solution {
public:
int lengthOfLastWord(string s) {
int N = s.size();
int left = 0, right = N - 1;
while (right >= 0 && s[right] == ' ') right--;
left = right;
while (left >= 0 && s[left] != ' ') left--;
return right - left;
}
};

单指针

使用一个指针也可以完成上面的操作。代码比较简单,不解释了。

Python版本如下:

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
N = len(s)
count = 0
for i in range(N - 1, -1, -1):
if s[i] == " ":
if count == 0:
continue
else:
break
else:
count += 1
return count

C++版本如下:

class Solution {
public:
int lengthOfLastWord(string s) {
int N = s.size();
int count = 0;
for (int i = N - 1; i >= 0; --i) {
if (s[i] == ' ') {
if (count != 0) {
break;
}
} else {
count++;
}
}
return count;
}
};

日期

2017 年 8 月 24 日
2018 年 11 月 24 日 —— 周日开始!一周就过去了~

【LeetCode】58. Length of Last Word 解题报告(Python & C++)的更多相关文章

  1. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

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

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

  3. Java [Leetcode 58]Length of Last Word

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

  4. LeetCode 58. Length of Last Word

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

  5. [leetcode]58. Length of Last Word最后一个词的长度

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

  6. 【LeetCode】748. Shortest Completing Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】819. Most Common Word 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...

  8. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  9. Leetcode 58 Length of Last Word 难度:0

    https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...

随机推荐

  1. BAT的一些题

    114.java中实现多态的机制是什么 答:重写,重载.方法的重写Overriding和重载Overloading是Java多态性的不同表现.  重写Overriding是父类与子类之间多态性的一种表 ...

  2. 学习java 7.19

    学习内容: 接口的组成中加入了默认方法,静态方法,私有方法 接口中默认方法:public default 返回值类型  方法名(参数列表){ } public default void show()  ...

  3. Linux信号1

    信号(signal)是一种软中断,他提供了一种处理异步事件的方法,也是进程间唯一的异步通信方式.在Linux系统中,根据POSIX标准扩展以后的信号机制,不仅可以用来通知某进程发生了什么事件,还可以给 ...

  4. 【编程思想】【设计模式】【创建模式creational】建造者模式builder

    Python版 https://github.com/faif/python-patterns/blob/master/creational/builder.py #!/usr/bin/python ...

  5. 对于HTML和XML的理解

    1.什么是HTML??? HTML就是 超文本标记语言(超文本含义:超过文本 --图片 .视频.音频. 超链接) 2.HTML作用 把网页的信息格式化的展现,对网页信息进行规范化展示 连接(https ...

  6. 🏆【Alibaba中间件技术系列】「RocketMQ技术专题」Broker配置介绍及发送流程、异常(XX Busy)问题分析

    参考资料 Rocketmq官网:http://rocketmq.apache.org/ Rocketmq的其它项目:https://github.com/apache/rocketmq-externa ...

  7. 分布式全局ID生成器原理剖析及非常齐全开源方案应用示例

    为何需要分布式ID生成器 **本人博客网站 **IT小神 www.itxiaoshen.com **拿我们系统常用Mysql数据库来说,在之前的单体架构基本是单库结构,每个业务表的ID一般从1增,通过 ...

  8. 在写易买网时产生的错误 JSTL标签库中<c:choose></c:choose>不能放JSP页面<!-- -->注释

    最近在使用JSTL标签库的<c:choose>标签时候,发现在该标签体中加了JSP的<!-- -->注释时,总是会显示报错信息.错误的信息如下: org.apache.jasp ...

  9. Java中利用正则表达式获取一个网页中的所有邮箱地址

    package cn.tms.ui; import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; im ...

  10. JavaXML解析的四种方法(连载)

    1. xml简介 XML:指可扩展标记语言, Extensible Markup Language:类似HTML.XML的设计宗旨是传输数据,而非显示数据. 一个xml文档实例: 1 <?xml ...