zoukankan      html  css  js  c++  java
  • [LeetCode#58]Length of Last Word

    Problem:

    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;
        }
    }
  • 相关阅读:
    mysql主从复制配置
    es去重 获取重复数据后保留最小值ID后进行批量删除
    java springboot mybatis控制台打印sql方法
    python生成requirements.txt
    hadoop基本介绍
    docker容器操作
    Exception caught evaluating condition: [java.lang.RuntimeException: Error creating extended parser class: null], action = [Fail the build]
    Return code is: 400, ReasonPhrase: Repository does not allow updating assets: maven-releases. -> [Help 1]
    zabbix自定义监控postgresql死亡元组,shell脚本比较小数位,shell脚本统计时间精确毫秒
    记一次postgresql的归档日志爆仓磁盘处理
  • 原文地址:https://www.cnblogs.com/airwindow/p/4793410.html
Copyright © 2011-2022 走看看