zoukankan      html  css  js  c++  java
  • leetcode 925. Long Pressed Name

    判定是否长按

    var isLongPressedName = function (name, typed) {
                var i = 1, j = 0, n = name.length, m = typed.length;
                var last = name[0], iCount = 1
                while (i < n || j < m) {
                    var el = name[i];
                    if (el !== last) {
                        if (iCount !== 0) {
                            let jCount = 0
                            // console.log("j", j, m)
                            while (j < m) {
                                console.log("内循环", last, typed[j], j)
                                if (typed[j] !== last) {
                                    break //跳到外循环
                                }
                                j++
                                jCount++
                            }
    
                            if (jCount < iCount) {
                                return false
                            }
                            if (j == m && i < n) {
                                return false
                            }
                        }
                        last = el
                        iCount = 1
                    } else {
                        console.log("累加", el)
                        iCount++
                    }
                    i++
                }
                return true
    
            };
    
            console.log(isLongPressedName("alex", "aaleex"))
            console.log(isLongPressedName("saeed", "ssaaedd"))
            console.log(isLongPressedName("pyplrz", "ppyypllr"))
    

    更精简的实现

    var isLongPressedName = function(name, typed) {
        let j = 0;
        
        for (let i = 0; i < typed.length; i++) {
            if(name[j] == typed[i]) j++;
        }
        
        return j == name.length;
    };
    

    另一个

    var isLongPressedName = function (name, typed) {
           let nlen = name.length, tlen = typed.length;
            if (nlen > tlen) return false;
            
            let i = 0, j = 0;
            while (i < nlen && j < tlen) {
                let  nc = name.charAt(i);
                let  tc = typed.charAt(j);
                if (nc == tc) {
                    i++;
                    j++;
                } else {
                    if (j == 0 || tc != typed.charAt(j - 1)) {
                        return false;
                    }
                    j++;
                }
            }
            
            return i == nlen;
     
    };
    
  • 相关阅读:
    【疯狂积累CSS】1:CSS基础
    阿里云服务器配置小程序用ssl证书
    阿里云服务器申请ssl安全证书
    PDO连接SQLServer2008(linux和windows)
    win7 PHP7.0的PDO扩展
    Apache+php+mysql win7 64位安装的几个注意事项
    PHP配置xdebug
    PHPExcel导出
    【设计模式】命令模式
    【maven学习笔记】 01 初见
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/12174341.html
Copyright © 2011-2022 走看看