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

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - 司徒正美

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 925. Long Pressed Name 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 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 844. Backspace String Compare
司徒正美 · 2020-01-01 · via 博客园 - 司徒正美

比较两个带退格键的字符串

方法1, 从后往前遍历,记录要忽略掉的字符

var backspaceCompare = function(S, T) {
    return getMormalString(S) === getMormalString(T)
};

function getMormalString(S){
     var eat = 0, sb = []
     for(var i = S.length-1; i>=0 ; i--){
         if(S[i] == '#'){
             eat++
         }else{
             if(eat > 0){
                 eat--
             }else{
                 sb.push(S[i])
             }
         }
     }
    return sb.join('')
}

方法2, 使用栈,从前往后处理

var backspaceCompare = function(S, T) {
     if (!S || !T || S.length === 0 || T.length === 0) {
        return false;
    }
    
    let stackS = [];
    let stackT = [];
    
    for (let i = 0; i < S.length; i++) {
        if (S[i] !== '#') {
            stackS.push(S[i]);
        } else {
            stackS.pop();
        }
    }
    
    for (let i = 0; i < T.length; i++) {
        if (T[i] !== '#') {
            stackT.push(T[i]);
        } else {
            stackT.pop();
        }
    }
    
    if (stackS.length !== stackT.length) {
        return false;
    } else {
        for (let i = 0; i < stackS.length; i++) {
            if (stackS[i] !== stackT[i]) {
                return false;
            }
        }
    }
    return true;
}

方法3,使用归并排序合并数组的形态,从后往前比较

var backspaceCompare = function(S, T) {
    let sPointer = S.length - 1;
    let tPointer = T.length - 1;
    let sBackspaces = 0;
    let tBackspaces = 0;

    while (sPointer >= 0 || tPointer >= 0) {
        if (S[sPointer] === '#') {
            sBackspaces++;
            sPointer--;
            continue;
        }
        
        if (T[tPointer] === '#') {
            tBackspaces++;
            tPointer--;
            continue;
        }
        
        if (sBackspaces > 0) {
            sBackspaces--;
            sPointer--;
            continue
        }

        if (tBackspaces > 0) {
            tBackspaces--;
            tPointer--;
            continue;
        }

        if (S[sPointer] !== T[tPointer]) {
            return false;
        }
        
        sPointer--;
        tPointer--;
    }
    
    return true;
};