题目:

给定一个字符串, 包含大小写字母、空格' ',请返回其最后一个单词的长度。

如果不存在最后一个单词,请返回 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 最后一个单词的长度的更多相关文章

  1. [Leetcode] Length of last word 最后一个单词的长度

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

  2. 058 Length of Last Word 最后一个单词的长度

    给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...

  3. [LintCode] Length of Last Word 求末尾单词的长度

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

  4. 58. Length of Last Word最后一个单词的长度

    [抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...

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

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

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

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

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

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

  8. [Swift]LeetCode58. 最后一个单词的长度 | Length of Last Word

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

  9. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

随机推荐

  1. IntelliJ IDEA+Tomcat+Nginx运行git项目

    1.克隆Git项目到本地 (1)设置Git工具路径:file>settings>Version Control>Git (2)设置GitHub账户:file>settings& ...

  2. webstorm 自定义代码模板

    Ctrl+Shift+A查找设置live 添加代码片段Live Template 编写代码的时候 缩写+Tab 即可输出代码片段 缩写启动对应代码片段的钥匙. 描述代码片段的名字. 模板文本代码片段的 ...

  3. Win7下MongoDB安装

    一.下载MongoDB 下载地址:http://www.mongodb.org/downloads 注意:1.从2.2开始,MongoDB不再支持windows xp. 2.32位MongoDB最大支 ...

  4. Js 和 PHP 中保留小数点后X位数的方法 toFixed、round、number_format、sprintf

    在 Javacript 中保留小数点后两位数的方法为 toFixed(2),其中的2为保留两位,写多少就保留多少了,满5进1. Javacript例子: var num = 24.54789523; ...

  5. R简易入门(二)

    本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据   原始数据展示(这是一份 ...

  6. 使用PowerShell 连接Azure

    除了使用门户登入外,还可以使用PowerShell的方式来连接Azure.首先要去下载组件 http://azure.microsoft.com/en-us/downloads/?rnd=1 http ...

  7. hdu 5738 2016 Multi-University Training Contest 2 Eureka 计数问题(组合数学+STL)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题意:从n(n <= 1000)个点(有重点)中选出m(m > 1)个点(选出的点只 ...

  8. jetty 8.x, 9.x无法加载jstl的PWC6188问题

    参考: cannot load JSTL taglib within embedded Jetty server:http://stackoverflow.com/questions/2151075/ ...

  9. 2391: Cirno的忧郁 - BZOJ

    Description Cirno闲着无事的时候喜欢冰冻青蛙.Cirno每次从雾之湖中固定的n个结点中选出一些点构成一个简单多边形,Cirno运用自己的能力能将此多边形内所有青蛙冰冻.雾之湖生活着m只 ...

  10. text-align:justify实现文本两端对齐布局,兼容IE

    要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清楚的概念.大多 IE下的显示错误,就是源于 haslayout. 什么是 haslayout ...