lintcode:Length of Last Word 最后一个单词的长度
题目:
给定一个字符串, 包含大小写字母、空格' '
,请返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0
。
给定 s = "Hello World"
,返回 5
。
一个单词的界定是,由字母组成,但不包含任何的空格。
解题:
利用正则确认是字母,向前走,当遇到不是字母的时候跳出程序,为了合适的跳出定义一个布尔值,是字母的时候是true,当不是字母同时布尔值是true时候跳出
Java程序:
public class Solution {
/**
* @param s A string
* @return the length of last word
*/
public int lengthOfLastWord(String s) {
// Write your code here
int lenword = 0;
boolean left = false;
String match = "\\w";
for(int i= s.length()-1;i>=0;i--){
String str = s.substring(i,i+1);
if(left==true &&str.matches(match)==false){
break;
}
if(str.matches(match)){
lenword+=1;
left = true;
} }
return lenword;
}
}
总耗时: 11524 ms
Python程序:
class Solution:
# @param {string} s A string
# @return {int} the length of last word
def lengthOfLastWord(self, s):
# Write your code here
lenword = 0
isAlpha = False
for i in range(len(s)-1,-1,-1):
tmp = s[i]
if isAlpha==True and tmp.isalpha()==False:
break
if tmp.isalpha():
isAlpha = True
lenword +=1
return lenword
总耗时: 483 ms
lintcode:Length of Last Word 最后一个单词的长度的更多相关文章
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- [LintCode] 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最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- [LeetCode] 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]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [Swift]LeetCode58. 最后一个单词的长度 | Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
随机推荐
- JS重写alert,保证弹窗错误的友好性
// ------------------------------------------------------------- // 重写alert,保证弹窗错误的友好性 var j_oldAler ...
- linux查看硬件信息的命令(图文)
发布:脚本学堂/Linux命令 编辑:JB02 2013-12-23 21:48:18 [大 中 小] 转自:http://www.jbxue.com/LINUXjishu/14996.htm ...
- jdbc 连接 mysql 获取 数据集 条数
package nona; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; im ...
- Ztack学习笔记(2)-系统初始化分析
main函数先执行初始化工作,包括硬件.网络层.任务等的初始化. 一 系统初始化 系统初始化函数主要完成内存分配.消息队列头.定时器.电源管理.任务系统及内存栈等的初始化,具体如下代码所示: //os ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
- OpenWRT 路由配置技巧
随着最近 Google 在国内已经完全无法访问,使得通过 VPN 访问网络的需求更加强烈,本文介绍的方法可以使一个普通的路由具备稳定连接 VPN 的能力,并能够根据目标访问网站选择国内外线路,从而得到 ...
- Redis 四:存储类型之列表类型
.lpush num 依次从左边推入0 - .rpush num 依次从右边推入0 - .lrnage num - 显示num列表中所有的数据 结果: .lpop num 从左边删除并弹出一个元素 . ...
- Win8.1 IIS6 SQL SERVER 2012 执行 SqlServices.InstallSessionState 出错
新装了WIN8.1,感觉很不错. 新建了第一个站点是,在执行 SqlServices.InstallSessionState("localhost", null, SessionS ...
- linux编码
转: Linux查看文件编码格式及文件编码转换 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而L ...
- Lightmapping
当游戏场景包含了大量的多边形时,实时光源和阴影对游戏性能的影响会很大.这时更适合使用Lightmapping技术,将光线效果预渲染成贴图使用到多边形上模拟光影效果.这种方式不用担心光源数量和阴影对性能 ...