题目:

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. python with语句上下文管理的两种实现方法

    在编程中会经常碰到这种情况:有一个特殊的语句块,在执行这个语句块之前需要先执行一些准备动作:当语句块执行完成后,需要继续执行一些收尾动作.例如,文件读写后需要关闭,数据库读写完毕需要关闭连接,资源的加 ...

  2. jvm 数据区划分学习

    Java virtual machine 运行时数据存储区域划分 2015年1月25日 19:15 Pc  寄存器 Each Java Virtual Machine thread has its o ...

  3. 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports

    http://www.cnblogs.com/shanyou/archive/2013/02/12/2910232.html SQL Server Performance Dashboard Repo ...

  4. ios应用启动后的自动版本检测方式

    今天意外的发现了appstore居然还提供通过url获取json格式的客户端信息链接: http://itunes.apple.com/lookup?id=$id 通过此地址可以获取应用的icon.介 ...

  5. 序列化form表单内容为json对象

    SourceCode: ; (function ($) { $.fn.extend({ serializeJson: function () { var json = {}; $(this.seria ...

  6. Entity Framework(序)

    ADO.NET Entity Framework 是一个对象-关系的映射结构,它提供了ADO.NET的一个抽象,可基于引用的数据库获取对象模型.可以通过Entity Framework 使用不同的变成 ...

  7. php文字水印和php图片水印实现代码(二种加水印方法)

    文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件.效果图如下: $dst_path = 'dst.jpg';//创建图片的实例$dst = imag ...

  8. JavaScript Tutorial

    JavaScript Tutorial http://javascript.info/root Object.create rabit.hasOwnProperty('eats') Object.ge ...

  9. Daily Scrum 11.11

    摘要:本次会议继续讨论程序的问题以及单元测试和集成测试,本次测试为1.02版本.本次的Task列表如下: Task列表 出席人员 Today's Task Tomorrow's Task 刘昊岩  t ...

  10. 从状态转移看:载波侦听多路访问/冲突避免(CSMA/CA)

    CSMA/CA是写入IEEE802.11的无线网络MAC层标准协议,相信看到这篇文章的读者都知道它是用来做什么的.但许多短文对这个协议的解释都有所缺乏,因此本文用状态转换图的形式详细说明协议的工作流程 ...