【LeetCode】58. Length of Last Word 解题报告(Python & C++)
作者: 负雪明烛
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++)的更多相关文章
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- [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 ...
- 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 ...
- 【LeetCode】748. Shortest Completing Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】819. Most Common Word 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leet ...
- LeetCode: 58. Length of Last Word(Easy)
1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...
- Leetcode 58 Length of Last Word 难度:0
https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...
随机推荐
- LeetCode最富有客户的资产总量
最富有客户的资产总量 题目描述 给你一个 m * n 的整数网格 accounts,其中 account[i][j]是第 i 位客户在第 j 家银行托管的资产数量.返回最富有客户所拥有的资产总量. 客 ...
- 转 Android Lifecycle、ViewModel和LiveData
转自:https://www.jianshu.com/p/982545e01d0a 1.概述 在I / O '17的时候,其中一个重要的主题是Architecture Components.这是一个官 ...
- Android 高级UI组件(三)
一.popupWindow 1.AlertDialog和PopupWindow最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManage ...
- java生成cron表达式
bean类: package com.cst.klocwork.service.cron; public class TaskScheduleModel { /** * 所选作业类型: * 1 -&g ...
- What happens when more restrictive access is given to a derived class method in C++?
We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive acc ...
- testng 执行多个suite
我们知道testng的配置文件,一个.xml里面只能有一个suite,那么如果想要设置多个suite怎么弄呢?这个时候我们需要用到testng的标签<suite-files>. 下面说一下 ...
- 《手把手教你》系列技巧篇(五十)-java+ selenium自动化测试-字符串操作-上篇(详解教程)
1.简介 自动化测试中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥这里介绍两种 ...
- Jsp/servlet分页五要素
分页5要素: * 1)pageIndex 当前页 * 2)startIndex 从第几条数据开始 * 3)countAll 总条目数 * 4)pageSize 每页大小 * 5)pageCount 总 ...
- Nginx 架构基础
1 Nginx请求处理流程 2 Nginx进程结构 3 Nginx进程管理:信号 3.1 Master进程 监控worker进程 CHLD 管理worker进程 接收信号 TERM,INT QUIT ...
- &和nohup
目录 一.简介 二.& 三.nohup 一.简介 当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我 ...