惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
T
Troy Hunt's Blog
N
News and Events Feed by Topic
雷峰网
雷峰网
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 最新话题
V
V2EX
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
I
Intezer
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
月光博客
月光博客
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
B
Blog
博客园 - 聂微东
P
Palo Alto Networks Blog
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
Scott Helme
Scott Helme
Google DeepMind News
Google DeepMind News
Webroot Blog
Webroot Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
O
OpenAI News
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
A
Arctic Wolf

博客园 - 司徒正美

leetcode 91. Decode Ways leetcode 1214 Two Sum BSTs leetcode 213 House Robber II leetcode 198 House Robber I leetcode 986. Interval List Intersections leetcode 869. Reordered Power of 2 leetcode 457. Circular Array Loop leetcode 1093. Statistics from a Large Sample leetcode 881. Boats to Save People leetcode 977. Squares of a Sorted Array leetcode 844. Backspace String Compare leetcode 1032. Stream of Characters leetcode 1023. Camelcase Matching leetcode 745 Prefix and Suffix Search leetcode 720. Longest Word in Dictionary leetcode 692. Top K Frequent Words leetcode 677. Map Sum Pairs leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 925. Long Pressed Name
司徒正美 · 2020-01-09 · via 博客园 - 司徒正美

判定是否长按

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;
 
};