题目:

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

如果不存在最后一个单词,请返回 0 。

样例

给定 s = "Hello World",返回 5

注意

一个单词的界定是,由字母组成,但不包含任何的空格。

解题:

利用正则确认是字母,向前走,当遇到不是字母的时候跳出程序,为了合适的跳出定义一个布尔值,是字母的时候是true,当不是字母同时布尔值是true时候跳出

Java程序:

  1. public class Solution {
  2. /**
  3. * @param s A string
  4. * @return the length of last word
  5. */
  6. public int lengthOfLastWord(String s) {
  7. // Write your code here
  8. int lenword = 0;
  9. boolean left = false;
  10. String match = "\\w";
  11. for(int i= s.length()-1;i>=0;i--){
  12. String str = s.substring(i,i+1);
  13. if(left==true &&str.matches(match)==false){
  14. break;
  15. }
  16. if(str.matches(match)){
  17. lenword+=1;
  18. left = true;
  19. }
  20.  
  21. }
  22. return lenword;
  23. }
  24. }

总耗时: 11524 ms

Python程序:

  1. class Solution:
  2. # @param {string} s A string
  3. # @return {int} the length of last word
  4. def lengthOfLastWord(self, s):
  5. # Write your code here
  6. lenword = 0
  7. isAlpha = False
  8. for i in range(len(s)-1,-1,-1):
  9. tmp = s[i]
  10. if isAlpha==True and tmp.isalpha()==False:
  11. break
  12. if tmp.isalpha():
  13. isAlpha = True
  14. lenword +=1
  15. 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. 【转载】DataGridView 使用集合作为数据源,并同步更新

    原文地址:http://hi.baidu.com/netyro/item/7340640e36738a813c42e239 今天做项目时遇到一个挠头的问题,当DataGridView的数据源为泛型集合 ...

  2. DataGridview动态添加列

    1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...

  3. MySQL基础操作命令

    MySQL基础操作命令 1. 查看MySQL进程 ps -ef|grep mysql |grep -v grep 2. 查看MySQL端口 ss -lnt | grep 3306 3. MySQL的启 ...

  4. this.IsMounted() is not a function

    I'm trying to build a simple React App. It retrieves data from an ajax call and renders it to the pa ...

  5. trade 主要前端组件

    jQuery Custombox http://www.jqueryfuns.com/resource/view/27

  6. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  7. IOS开发一些资源收集

    从别的地方看到的,多谢作者,现贴在这里备忘. 在线教程 iOS技术概述    我个人感觉这是开始iOS开发第一步,了解一下iOS整体的结构,有哪些支持类库.如何使用类库等等一切基础的概念性指示 iPh ...

  8. 【BZOJ 1005】[HNOI2008]明明的烦恼

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...

  9. js之正则表达式(下)

    1.分组之exec返回数组 1>非分组匹配的exec返回数组: var pattern =/\d+[a-z]+/; var str='234google'; alert(pattern.exec ...

  10. 屏蔽ios7中某个页面的默认手势滑回返回

    - (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...