[LeetCode#58]Length of Last Word
2015-09-09 00:09
330 查看
Problem:
Given a string s consists of upper/lower-case alphabets and empty space characters
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 =
return
Analysis:
Solution:
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.
Analysis:
This is very typical problem of after count. (only at the next step, we know wheather the previous count should be recorded) For this problem, we can't determine if the current character is the last character of a word. We can only make such decision when the we know the next character is boundary or space character ' '. For this kind of problem, we need to carefully design the following things: problem 1. the start condition, since we need to maintain a count, what's the intial value of the count? problem 2. how do we decide to record a qualifed word? when do we update the global record? For problem 1, for this problem, there could be empty space at any place, even at s.charAt(0), we should only make a count when we are sure the current character is not empty space. Thus the intial value of count should be 0. For problem 2, the instant idea is to use ' ' as a sign to record previous word's information. if (s.charAt(i) == ' ') { len = count; count = 0; } It looks simple, but it could incur complex logic problem. 1. What if there are multiple ' ' after a word? "jay " What would you do? Increase the checking over wheather the empty space's previous character is ' ' ? (why? too complex) 2. What if there are no empty space after a word? "jay" Those two uncertainties could make our code ugly and hard to maintain, Can we simplify it? Yes. Since empty space is just a way of separating word, it could appear at any indexes and multi times, Why should we care about it? 3. How about use following checking condition? 3.1. If the current character is not ' ' and, it is previous character is ' ', apparenlty we should record the previous word's length. 3.2 If the current character is not ' ', and it is previous character is not ' ', apparenlty we are still in a word, we should just increase the count. if (s.charAt(i) != ' ') { if (i == 0 || s.charAt(i-1) != ' ') { count++; } else{ len = count; count = 1; } } else{ continue; } Note: a. since when i is 0, it has no previous character, we have: if (i == 0 || s.charAt(i-1) != ' ') { count++; } b. After counting must consider the case the last word was not counted. for (int i = 0; i < s.length(); i++) { ... } len = count;
Solution:
public class Solution { public int lengthOfLastWord(String s) { if (s == null) throw new IllegalArgumentException("s is null"); int len = 0; int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != ' ') { if (i == 0 || s.charAt(i-1) != ' ') { count++; } else{ len = count; count = 1; } } else{ continue; } } len = count; return len; } }
相关文章推荐
- [其它招式]强大的VBS,自动化利器
- 线程与进程初理解!
- 第一天
- C#中的多线程 - 同步基础
- unity3d AssetBundle包加密
- 博客已搬家至 :qinshaoxuan.com
- 创业失败
- Oracle_启动报ora-27100:shared memory realm already exists
- springmvc处理静态资源
- VTD-XML——解析xml的最强武器
- addActionError() 与addFieldError (),addActionMessage()比较
- Hadoop(四)——编程核心MapReduce(上)
- MYSQL数据库学习----索引和触发器
- uva 1632 阿里巴巴
- Unity bundle的制作和使用
- 全局脚本
- 常用DDR和NandFlash型号
- Linux命令备忘实例(9)——参数代换xargs
- 全局脚本
- UVALive 3942 Remember the Word(字典树 + 简单dp)