• 58. Length of Last Word最后一个单词的长度


    [抄题]:

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    "b a "

    最后一位是空格,可能误判lastindexof()。所以必须用.trim()

    [思维问题]:

    [一句话思路]:

    用函数 再次强调是最后一位的索引是length() - 1

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    要用trim()排除特殊情况

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

    public class Solution {
        /**
         * @param s: A string
         * @return: the length of last word
         */
        public int lengthOfLastWord(String s) {
            // write your code here
            return s.trim().length() - 1 - s.trim().lastIndexOf(" ");
        }
    }
    View Code

     [代码风格] :

  • 相关阅读:
    First duplicate value
    SQL学习笔记day1
    Find closest value in BST
    BST construction
    Closest sum_pair
    滑动窗口 sliding window
    设计模式(3)观察者模式
    设计模式(1)装饰模式总结
    深刻探讨public class=new class();
    与时间赛跑,我的2012
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8613502.html
Copyright © 2020-2023  润新知