lintcode 29. 交叉字符串
来源:互联网 发布:淘宝美工助理注册码 编辑:程序博客网 时间:2024/06/05 19:13
描述
给出三个字符串:s1、s2、s3,判断s3是否由s1和s2交叉构成。
样例
比如 s1 = “aabcc” s2 = “dbbca”
- 当 s3 = "aadbbcbcac",返回 true.- 当 s3 = "aadbbbaccc", 返回 false.
思路
把i和j作为s1和s2的遍历指针,k作为s3的遍历指针,使用深度优先搜索,比较s1[i]和s3[k]以及s2[j]和s3[k]。
代码
#include<map>#include<vector>class Solution {public: /* * @param s1: A string * @param s2: A string * @param s3: A string * @return: Determine whether s3 is formed by interleaving of s1 and s2 */ string s1,s2,s3; bool a[1001][1001]={{false}}; bool flag=false;//用来标记s3是否到末尾 bool isInterleave(string &s1, string &s2, string &s3) { // write your code here this->s1=s1;this->s2=s2;this->s3=s3; dist(0,0,0); return flag; } void dist(int i,int j,int k){ if(flag||a[i][j]){ return; } if(s3[k]=='\0'){ flag=true; return; } a[i][j]=true; if(s1[i]==s3[k]){ dist(i+1,j,k+1); } if(s2[j]==s3[k]){ dist(i,j+1,k+1); } }};
阅读全文
0 0
- lintcode 29. 交叉字符串
- LintCode-交叉字符串
- LintCode(M)交叉字符串
- LintCode 29-交叉字符串
- LintCode 29 交叉字符串
- LintCode 交叉字符串
- lintcode -- 交叉字符串
- LintCode 交叉字符串
- lintcode interleaving-string 交叉字符串
- [Lintcode] Interleaving String 交叉字符串
- (lintcode)第29题交叉字符串
- lintcode 交叉链表
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 交叉字符串
- 随笔
- 求数列的和
- fleury算法输出欧拉回路
- Integer与int比较
- 654. Maximum Binary Tree
- lintcode 29. 交叉字符串
- JSP九大内置对象的作用和用法总结
- GDB入门教程
- CString 文件修改指定位置的字符串(文件大小不变)
- LeetCode 1. Two Sum
- 新开始
- tensorflow RNN LSTM语言模型
- 时钟
- PHP MVC