LeetCode:Interleaving String
来源:互联网 发布:淘宝开发团队 编辑:程序博客网 时间:2024/05/01 12:29
二维dp
dp[i][j] 表示是否能有s1[0...i-1]和[s2[0....j-1]组成s3【0.....i+j-1】
class Solution {public: bool isInterleave(string s1, string s2, string s3) { // Start typing your C/C++ solution below // DO NOT write int main() function int len1 = s1.length(); int len2 = s2.length(); int len3 = s3.length(); if(len1+len2!=len3) return false; if(len1==0&&s2!=s3) return false; if(len2==0&&s1!=s3) return false; bool** dp = new bool *[len1+1]; for(int i =0;i<=len1;i++) dp[i] = new bool[len2+1]; dp[0][0] = 1; for(int i =1;i<=len1;i++) if(dp[i-1][0]&&s1[i-1]==s3[i-1]) dp[i][0] = true; else dp[i][0] = false; for(int i =1;i<=len2;i++) if(dp[0][i-1]&&s2[i-1]==s3[i-1]) dp[0][i] = true; else dp[0][i] = false; for(int i =1;i<=len1;i++) for(int j = 1;j<=len2;j++) if(dp[i-1][j]&&s1[i-1] == s3[i+j-1]) dp[i][j] =1; else if(dp[i][j-1]&&s2[j-1] == s3[i+j-1]) dp[i][j] =1; else dp[i][j]=0; return dp[len1][len2]; }};
- LeetCode: Interleaving String
- LeetCode Interleaving String
- LeetCode: Interleaving String
- [Leetcode] Interleaving String
- [LeetCode] Interleaving String
- leetcode 68: Interleaving String
- [Leetcode] Interleaving String
- LeetCode - Interleaving String
- [LeetCode]Interleaving String
- LeetCode: Interleaving String
- Leetcode: Interleaving String
- [Leetcode]Interleaving String
- Leetcode: Interleaving String
- LeetCode:Interleaving String
- LeetCode-Interleaving String
- [leetcode] Interleaving String@DP
- LeetCode:Interleaving String
- LeetCode - Interleaving String
- Java中获取键盘输入值的三种方法(http://soft.chinabyte.com/database/191/12466191.shtml)
- CompressionModule,让ASP.NET支持Gzip压缩输出
- 轻松搞定面试中的二叉树题目
- 催稿信
- 具有Flash Access保护的HTTP Dynamic Streaming
- LeetCode:Interleaving String
- Hadoop+GPU强强联手的性能探索
- javascript ajax 脚本跨域调用全解析
- hdu 1576 扩展欧几里得
- Padrino 生成器指南
- SPFA算法
- ff activex ocx 有待补充,有待验证
- WinMain的两个参数hInstance与hPrevInstance
- [LeetCode题解001]Two Sum