题目:

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.

Hide Tags

String

 

链接: http://leetcode.com/problems/length-of-last-word/

题解:

从后向前遍历。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public int lengthOfLastWord(String s) {
if(s == null || s.length() == 0)
return 0;
int i = s.length() - 1, count = 0;
s = s.toLowerCase(); while(i >= 0 && (s.charAt(i) > 'z' || s.charAt(i) < 'a') )
i --; while(i >= 0 && s.charAt(i) != ' '){
count ++;
i --;
} return count;
}
}

Update:

看一看以前的代码...完全不认识了...

public class Solution {
public int lengthOfLastWord(String s) {
int count = 0;
if(s == null || s.length() == 0)
return count; for(int i = s.length() - 1; i >= 0; i--) {
if(s.charAt(i) == ' ') {
if(count == 0)
continue;
else
break;
}
count++;
} return count;
}
}

二刷:

还是跟一刷一样,从后向前遍历。

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public int lengthOfLastWord(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ') {
count++;
} else if (count > 0) {
break;
}
}
return count;
}
}

三刷:

这道题属于基本很少能给人留下印象的题。从后向前遍历, 假如s.charAt(i)不为空格的时候,我们增加count, 否则,假如count = 0,我们还没找到单词,继续查找。 假如count > 0,说明我们已经找到并且计算了最后一个单词,返回count。 遍历数组以后也返回count,因为有可能字符串里就一个单词,没空格。 这分析写得比较糙...

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public int lengthOfLastWord(String s) {
if (s == null) {
return 0;
}
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ') {
count++;
} else if (count > 0) {
return count;
}
}
return count;
}
}

58. Length of Last Word的更多相关文章

  1. LeetCode练题——58. Length of Last Word

    1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...

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

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

  3. LeetCode 58. Length of Last Word

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

  4. 【LeetCode】58 - Length of Last Word

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

  5. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  6. 58. Length of Last Word【leetcode】

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

  7. &lt;LeetCode OJ&gt; 58. Length of Last Word

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

  8. 58. Length of Last Word(easy, 字符串问题)

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

  9. 【一天一道LeetCode】#58. Length of Last Word

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

随机推荐

  1. IIS6下, web.config配置为targetFramework="4.0"时出404错误

    打开IIS管理器,在"Web 服务扩展" 中 将ASP.NET v4.0设置为允许就好了.这个选项默认是禁止的.

  2. Oracle 表的访问方式(1) ---全表扫描、通过ROWID访问表

    1.Oracle访问表的方式 全表扫描.通过ROWID访问表.索引扫描 2.全表扫描(Full Table Scans, FTS) 为实现全表扫描,Oracle顺序地访问表中每条记录,并检查每一条记录 ...

  3. Selenium-RC Python 2.7 环境配置

    1.下载并安装Python http://www.python.org/getit/,我使用的是2.7.3的python版本 2.下载并安装setuptools[这个工具是python的基础包工具] ...

  4. Java中resourceBundle和Properties的区别

    第一种办法InputStream is = Test.class.getResourceAsStream("DbConfig.properties");Properties p = ...

  5. tomcat中server.xml文件解析

    下面我们将讲述这个文件中的基本配置信息,更具体的配置信息见tomcat的文档 元素名 属性 解释 server port 指定一个端口,这个端口负责监听关闭tomcat的请求 shutdown 指定向 ...

  6. 初涉Node.js

    Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速,可扩展的网络应用程序的平台. Node.js使用事件驱动.非阻塞I/ O模型,是轻量级.高效.完美的跨分布式设备运 ...

  7. Notes of the scrum meeting(2013/10/23)

    ps:本来是10月23号周三下午开的会,这几天由于各种事情忙,忘记写博客了,现在补上. 软工项目组buaa_smile开始项目第一次scrum meeting meeting time:4:00~5: ...

  8. Win64位操作系统无法运行暗黑2战网D2GS的解决办法

    前几天想在我的Win7 x64系统里做个战网自己玩,搭建完毕后进入战网创建房间出现经典的问题,“排队1”. 原因很清楚,就是D2GS无法启动:但是使用之前的各种办法尝试后无果,后来查看D2GS同目录下 ...

  9. C# 该行已经属于另一个表 的解决方法[转]

    该文转自:http://blog.sina.com.cn/s/blog_48e4c3fe0100nzs6.html DataTable dt = new DataTable(); dt = ds.Ta ...

  10. java 连接池的简单实现

    最近一个项目中需要自己写个连接池, 写了一个下午,挺辛苦的,但不知道会不会出问题, 所以,贴到博客上,欢迎各路大神指点 1. 配置信息: /** * */ package cn.mjorcen.db. ...