LeetCode 97: Interleaving String 字串交錯,翻轉字串leetcode

來源:互聯網
上載者:User

LeetCode 97: Interleaving String 字串交錯,翻轉字串leetcode

部落格轉載請註明地址:http://blog.csdn.net/SunliyMonkey/article/details/48165711

題目描述

題目地址:https://leetcode.com/problems/interleaving-string/
給定三個字串S1,S2,S3,判斷S3是否能夠由S1,S2交錯組合而成。

範例:

S1=aabcc
S2=dbbca
當S3=aadbbcbcac時,返回 true
當S3=aadbbbaccc時,返回 false

考察點陷阱

無

Code
class Solution {public:    bool isInterleave(string s1, string s2, string s3) {       vector <int> match[2];        int cur, nxt;        int l;        int l1 = s1.length();        int l2 = s2.length();        int l3 = s3.length();        if(l1+l2 != l3)            return false;        int *vis[2];        vis[0] = new int[l1+1];        vis[1] = new int[l1+1];        for(int i = 0; i <= l1; i++)            vis[0][i] = vis[1][i] = -1;        nxt = 0;        match[0].push_back(0);        for(int i = 0; i < l3; i++){            cur = nxt;            nxt = !nxt;            match[nxt].clear();            for(int k = 0; k < match[cur].size(); k++){                l = match[cur][k];                if(l < l1 && s1[l] == s3[i]){                    if(vis[nxt][l+1] != i){                        match[nxt].push_back(l+1);                        vis[nxt][l+1] = i;                    }                }                if(i-l+1 <= l2 && s2[i-l] == s3[i]){                    if(vis[nxt][l] != i){                        match[nxt].push_back(l);                        vis[nxt][l] = i;                    }                }            }        }        return !match[nxt].empty();    }};
排名

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。 部落格原址:http://blog.csdn.net/sunliymonkey

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.