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 ...
随机推荐
- 【转载】DataGridView 使用集合作为数据源,并同步更新
原文地址:http://hi.baidu.com/netyro/item/7340640e36738a813c42e239 今天做项目时遇到一个挠头的问题,当DataGridView的数据源为泛型集合 ...
- DataGridview动态添加列
1.获取数据源(select * from table名称) 2.动态绑定数据源 private void GetTableInfo(DataTable dt) { listBh = new List ...
- MySQL基础操作命令
MySQL基础操作命令 1. 查看MySQL进程 ps -ef|grep mysql |grep -v grep 2. 查看MySQL端口 ss -lnt | grep 3306 3. MySQL的启 ...
- 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 ...
- trade 主要前端组件
jQuery Custombox http://www.jqueryfuns.com/resource/view/27
- python time模块和datetime模块详解
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...
- IOS开发一些资源收集
从别的地方看到的,多谢作者,现贴在这里备忘. 在线教程 iOS技术概述 我个人感觉这是开始iOS开发第一步,了解一下iOS整体的结构,有哪些支持类库.如何使用类库等等一切基础的概念性指示 iPh ...
- 【BZOJ 1005】[HNOI2008]明明的烦恼
Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...
- js之正则表达式(下)
1.分组之exec返回数组 1>非分组匹配的exec返回数组: var pattern =/\d+[a-z]+/; var str='234google'; alert(pattern.exec ...
- 屏蔽ios7中某个页面的默认手势滑回返回
- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...