leetcode: Interleaving String
来源:互联网 发布:如何识别淘宝出售假货 编辑:程序博客网 时间:2024/06/06 09:40
采取二维数组动态规划...
flag[i][j]代表s1的前i-1个元素和s2的前j-1个元素能否构成s3的前i+j-1.....那么这个值由两方面决定:若s1[i-1]==s3[i+j-1],则要考虑flag[i-1][j];若s2[j-1]==s3[i+j-1],则要考虑flag[i][j-1];有可能以上两个条件同时存在,所以每次还要考虑flag[i][j]本身的值;若都不存在,则false;
要注意的是flag数组实现要设定flag[0][0]=true;
public class Solution { public boolean isInterleave(String s1, String s2, String s3) { int l1 = s1.length(); int l2 = s2.length(); int l3 = s3.length(); if(l1+l2!=l3) { return false; } boolean flag[][] = new boolean[l1+1][l2+1]; flag[0][0] = true;a for(int i=1;i<=l1;i++) { if(s1.charAt(i-1)==s3.charAt(i-1)) { flag[i][0]=true; } else { break; } } for(int i=1;i<=l2;i++) { if(s2.charAt(i-1)==s3.charAt(i-1)) { flag[0][i]=true; } else { break; } } for(int i=1;i<=l1;i++) { for(int j=1;j<=l2;j++) { int k=i+j; if(s1.charAt(i-1)==s3.charAt(k-1)) { flag[i][j] = flag[i-1][j]||flag[i][j]; } if(s2.charAt(j-1)==s3.charAt(k-1)) { flag[i][j] = flag[i][j-1]||flag[i][j]; } } } return flag[l1][l2]; }}
0 0
- 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
- Perl中的程序注释:单行注释和多行注释
- 正则表达式 简明版
- java的4种代码块
- TexturePacker 算法
- 管理好企业中的“老人”也是门艺术
- leetcode: Interleaving String
- IOS上路_01-Win7+VMWare9+MacOSX10.8+XCode4.6.3
- MySQL优化大全
- codechef Chef and easy problem
- qt 自定义外形 qpushbutton
- GitHub创建SSH Keys
- virtualbox 共享目录设置
- linux find命令格式及find命令详解
- CheckBoxList多条件取值,sql查询