leetcode_[python/C++]_97_Interleaving String_动态规划
2016-11-09 18:01
483 查看
题目链接
【题目】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = “aabcc”,
s2 = “dbbca”,
When s3 = “aadbbcbcac”, return true.
When s3 = “aadbbbaccc”, return false.
Recurse:
Judge Small: Accepted!
Judge Large: Time Limit Exceeded
(1)递归的思想,(Time error)
(2)动态规划:
建立二维数组,可达为1,不可达为0,不断往右往下,如果能走到右下角,则return true
s1 = “aabcc”
s2 = “dbbca”
s3 = “aadbbcbcac”
python
动态规划:
【题目】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = “aabcc”,
s2 = “dbbca”,
When s3 = “aadbbcbcac”, return true.
When s3 = “aadbbbaccc”, return false.
Recurse:
Judge Small: Accepted!
Judge Large: Time Limit Exceeded
(1)递归的思想,(Time error)
class Solution { public: bool isInterleave(string s1, string s2, string s3) { if(s1.length() == 0) return s2 == s3; if(s2.length() == 0) return s1 == s3; if(s3.length() == 0) return s1.length() + s2.length() == 0; if(s1[0] == s3[0] && s2[0] != s3[0]) return isInterleave(s1.substr(1),s2,s3.substr(1)); else if(s1[0] != s3[0] && s2[0] == s3[0]) return isInterleave(s1,s2.substr(1),s3.substr(1)); else if(s1[0] == s3[0] && s2[0] == s3[0]) return isInterleave(s1,s2.substr(1),s3.substr(1)) || isInterleave(s1.substr(1),s2,s3.substr(1)); else return false; } };
(2)动态规划:
建立二维数组,可达为1,不可达为0,不断往右往下,如果能走到右下角,则return true
s1 = “aabcc”
s2 = “dbbca”
s3 = “aadbbcbcac”
class Solution { public: bool isInterleave(string s1, string s2, string s3) { int size1 = s1.length(); int size2 = s2.length(); int size3 = s3.length(); if(size1 + size2 != size3) return false; int dp[size1+1][size2+1] = {0}; for( int i = 0 ; i <= size1 ; i ++ ) for( int j = 0 ; j <= size2 ; j ++ ) { if( i == 0 && j == 0 ) dp[i][j] = 1; else if( i > 0 && dp[i-1][j] && s3[i+j-1] == s1[i-1]) dp[i][j] = 1; else if( j > 0 && dp[i][j-1] && s3[i+j-1] == s2[j-1]) dp[i][j] = 1; else dp[i][j] = 0; } return dp[size1][size2]; } };
python
动态规划:
class Solution(object): def isInterleave(self, s1, s2, s3): size1 = len(s1) size2 = len(s2) size3 = len(s3) if size1 + size2 != size3: return False dp = [[0]*(size2 + 1) for i in range(size1 + 1)] for i in range(size1 + 1): for j in range(size2 + 1): if i == 0 and j == 0: dp[i][j] = 1 elif i > 0 and dp[i-1][j] and s3[i + j - 1] == s1[i - 1]: dp[i][j] = 1 elif j > 0 and dp[i][j -1] and s3[i + j - 1] == s2[j - 1]: dp[i][j] = 1 else: dp[i][j] = 0 return True if dp[size1][size2] else False
相关文章推荐
- LeetCode 97 — Interleaving String (C++ Python)
- LeetCode[97]::Interleaving String C++
- LeetCode 97 Interleaving String(Python详解及实现)
- LeetCode(97) Interleaving String(动态规划)
- [LeetCode 97] Interleaving String(Python)
- LeetCode(97) Interleaving String
- [leetcode-97]Interleaving String(java)
- leetcode[97]Interleaving String
- leetcode[97] Interleaving String
- leetcode_c++:Interleaving String(097)
- LeetCode 97: Interleaving String 字符串交错
- LeetCode(97) Interleaving String
- 【LeetCode with Python】 Interleaving String
- leetcode_question_97 Interleaving String
- LeetCode Interleaving String(动态规划)
- [Leetcode]97. Interleaving String @python
- LeetCode(97) Interleaving String
- LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
- 99. Interleaving String Leetcode Python
- leetcode || 97、Interleaving String