【leetcode】Length of Last Word
题目简述
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.
For example,
Given s = "Hello World",
return 5.
解题思路:
注意空格就好
class Solution:
# @param s, a string
# @return an integer
def lengthOfLastWord(self, s):
if len(s) == 0:
return 0
i = len(s) -1
while s[i] == ' ':
if i == 0:
return 0
i -= 1
res = s[0:i+1].split(' ')
return len(res[-1])
【leetcode】Length of Last Word的更多相关文章
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
- 【leetcode】length of last word (easy)
题目: 输入字符串 s,返回其最后一个单词的长度 如 s="Hello World" 返回5 s="Hello World " 返回5 s=&qu ...
- 【Leetcode】【Easy】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】Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...
- 【leetcode】1003. Check If Word Is Valid After Substitutions
题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
随机推荐
- Thinkphp文件上传
1.在IndexController.class.php里面写2个方法,shangchuan用来显示页面,upload是上传文件的方法. <?php namespace Home\Control ...
- linux 系统、命令、软件
软件名称:Putty 使用方法:http://jingyan.baidu.com/article/e73e26c0eb063324adb6a737.html 需要资料: 服务IP:228.5624.5 ...
- yii2 可逆加密
加密: $data 是你要加密的内容, $secretKey 是你自己设置的salt, $encryptedData = Yii::$app->getSecurity()->encrypt ...
- 比特币_Bitcoin 简介
2008-11 Satoshi Nakamoto Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...
- jQuery的选择器中的通配符[id^='code'] 等示例及说明
1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code']&quo ...
- 网页插件学javascript还是jquery好啊?
文章的起因,也是在群内交流是回答一个小伙的问题,一扯就停不下来,但由于个人知识面覆盖有限,自身基础又不够扎实,仅供参考: 问这个问题之前,我个人建议先搞清什么是jquery,什么是js? jq ...
- ELK 集中日志分析 windows部署实战
一步步来 1.下载软件 Elasticsearch: https://download.elasticsearch.org/...p/elasticsearch/2.0.0/elasticsearch ...
- mfc对话询问窗体
// 显示消息对话框 nRes = MessageBox(_T("您确定要跳转网站?"), _T("提示"), MB_OKCANCEL | MB_I ...
- JDBC之存储过程
存储过程的语法创建就不说了,这里这篇博客 就挺详细了http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html. 1. Java代码调用没有参数的存错过程 ...
- Eclipse的快捷键
Ctrl+Shift+L调出eclipse的所有快捷键 不定期更新2016#8#19 如何配置快捷键:windown-preferences-General-key 一些操作 Ctrl+D 删除 ...