作者: 负雪明烛
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. 利用vcftools比较两个vcf文件

    因为最近有一项工作是比较填充准确性的,中间有用到vcftools比较两个vcf文件. 使用命令也很简单: 1 vcftools --vcf file1.snp.vcf --diff file2.snp ...

  2. makefile高级用法

    在某些文件里面找.o find ./ -name "*.o" -e ls- R 看文件目录下面的文件 嵌套 和 VPATH [1]VPATH的用法 (1)VPATH:虚路径 1) ...

  3. tomcat拦截特殊字符报400,如 "|" "{" "}" ","等符号的解决方案

    最近在做一个项目,需要对外暴露两个接口接收别人给的参数,但是有一个问题就是对方的项目是一个老项目,在传参数的时候是将多个字符放在一个参数里面用"|"进行分割,然而他们传参数的时候又 ...

  4. day20 系统优化

    day20 系统优化 yum源的优化 yum源的优化: 自建yum仓库 使用一个较为稳定的仓库 # 安装华为的Base源 或者使用清华的源也可以 wget -O /etc/yum.repos.d/Ce ...

  5. 使用 Addressables 来管理资源

    使用 Addressables 来管理资源 一.安装 打开Package Manager,在Unity Technologies的目录下找到Addressables,更新或下载. 二.配置 依次打开W ...

  6. JS模块化,Javascript 模块化管理的历史

    模块管理这个概念其实在前几年前端度过了刀耕火种年代之后就一直被提起. 直接回想起来的就是 cmd amd commonJS 这三大模块管理的印象.接下来,我们来详细聊聊. 一.什么是模块化开发 为了让 ...

  7. k8s配置中心-configmap,Secret密码

    目录 k8s配置中心-configmap,Secret 创建ConfigMap 使用ConfigMap subPath参数 Secret 官方文档 编写secret清单 使用secret 在 Pod ...

  8. vim一键整理代码命令

    vim下写代码超实用代码格式整理命令,仅需四步 ①先使用 gg 命令使光标回到第一行 ②shift+v 进入可视模式 ③shift+g 全选 ④按下  =  即可 混乱的代码格式 四步整理以后 工整又 ...

  9. C++基本函数的调用优化(构造、拷贝构造、赋值)

    合理的函数可提升时间和空间的利用率 //Test1.h #include<iostream> using namespace std; struct ST { private: int a ...

  10. 什么是maven(一)

    转自博主--一杯凉茶 我记得在搞懂maven之前看了几次重复的maven的教学视频.不知道是自己悟性太低还是怎么滴,就是搞不清楚,现在弄清楚了,基本上入门了.写该篇博文,就是为了帮助那些和我一样对于m ...