题目:

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. Windows7鼠标右键里没有新建文本文件的选项,解决办法

    1.“开始”->“运行”,输入"regedit",打开注册表编辑器 2.展开HKEY_CLASSES_ROOT,找到.txt 3.选中.txt,查看右侧窗格的“默认值”是不是 ...

  2. Python中Cookie的处理(一)Cookie库

    Cookie用于服务器实现会话,用户登录及相关功能时进行状态管理.要在用户浏览器上安装cookie,HTTP服务器向HTTP响应添加类似以下内容的HTTP报头: Set-Cookie:session= ...

  3. Windows Server 2008 R2 64bit兼容Chrome浏览器

    近日更换系统Windows Server 2008 R2 64bit系统,发现谷歌浏览器插件无法正常运行,终于找到如下解决方案: 打开桌面谷歌浏览器属性,将target目标 C:\Users\Admi ...

  4. [转]unable to resolve superclass of 的奇怪问题和一种解决方法!

    [转]unable to resolve superclass of 的奇怪问题和一种解决方法! http://blog.csdn.net/jackymvc/article/details/90015 ...

  5. iOS10 关于推送-b

    最近在研究iOS10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点: 推送内容更加丰富,由之前的alert 到现在的title, subtitle, body 推送统一由tri ...

  6. C# ASP.NET系统缓存全解析

    原文:http://blog.csdn.net/wyxhd2008/article/details/8076105 目录(?)[-] 系统缓存的概述 页面输出缓存 页面局部缓存 文件缓存依赖 数据库缓 ...

  7. 如何用jmeter对websock和protobuf进行压力测试

    1. 一个websocket插件官网地址 https://github.com/maciejzaleski/JMeter-WebSocketSampler 2. 可以用上述插件,也可以自己扩展,以实现 ...

  8. CROSSTOOL-NG建立交叉编译工具链

    CROSSTOOL-NG建立交叉编译工具链 因为考试和学习的原因我已经一段时间没有玩我的JZ2440,现在终于考完试了,我再次找出了我的JZ2440.我之前学习的时候使用的是韦东山老师提供的开发工具, ...

  9. asp.net asp:TextBox控件绑定值后,获取不到新值问题解决方法

    把Page_Load里绑定的代码放在    if(!IsPostBack){}里面后,即可获取到更新的值. 意思为第一次加载执行.

  10. 2005: [Noi2010]能量采集 - BZOJ

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...