LintCode 29---交叉字符串
- public class Solution {
- /**
- * @param s1: A string
- * @param s2: A string
- * @param s3: A string
- * @return: Determine whether s3 is formed by interleaving of s1 and s2
- */
- public boolean isInterleave(String s1, String s2, String s3) {
- if(s1.length() + s2.length() != s3.length())
- return false;
- boolean[][] matched= new boolean[s1.length()+1][s2.length()+1];
- matched[0][0]= true;
- for(int i=1;i<= s1.length(); i++){
- if(s3.charAt(i-1) == s1.charAt(i-1))
- matched[i][0] = true;
- }
- for(int j= 1;j<= s2.length();j++){
- if(s3.charAt(j-1) == s2.charAt(j-1))
- matched[0][j] = true;
- }
- for(int i=1;i<=s1.length(); i++){
- char c1 = s1.charAt(i-1);
- for(int j = 1;j<= s2.length();j++){
- int i3 = i+ j;
- char c2 = s2.charAt(j- 1);
- char c3 = s3.charAt(i3 -1);
- if(c1 == c3)
- matched[i][j] =matched[i][j] || matched[i-1][j];
- if( c2== c3)
- matched[i][j] = matched[i][j] || matched[i][j-1];
- }
- }
- return matched[s1.length()][s2.length()];
- }
- }
LintCode 29---交叉字符串的更多相关文章
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- LintCode——交叉字符串
描述:给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例:s1 = "aabcc" s2 = "dbbca" - 当 s3 = &quo ...
- 交叉字符串 · Interleaving String
[抄题]: 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成.(洗牌) 比如 s1 = "aabcc" s2 = "dbbca" - 当 s3 ...
- hihoCoder2月29日(字符串模拟)
时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- 【LintCode】转换字符串到整数
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...
- 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators
backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- LintCode 55 比较字符串
比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 样例 给出 A = "ABC ...
- Lintcode--006(交叉字符串)
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...
随机推荐
- ILSpy C# language support status
C# language support status Asynchronous methods 已经支持 Generalized async return types 还不支持 Async main ...
- centos6一键安装WordPress
#!/bin/bash service iptables stop setenforce yum -y install httpd service httpd restart yum -y insta ...
- Android jni/ndk编程二:jni数据类型转换(primitive,String,array)
一.数据类型映射概述 从我们开始jni编程起,就不可能避开函数的参数与返回值的问题.java语言的数据类型和c/c++有很多不同的地方,所以我们必须考虑当在java层调用c/c++函数时,怎么正确的把 ...
- 【React自制全家桶】三、React使用ref操作DOM与setState遇到的问题
在React中同时使用ref操作DOM与setState常常会遇到 比如操作的DOM是setState更新之前的DOM内容,与想要的操作不一致.导致这样的原因是setState函数是异步函数. 就是当 ...
- C代码输出日志
模板代码,在实际开发中可以使用: Android.mk文件增加(放到 include $(CLEAR_VARS)下面) LOCAL_LDLIBS += -llog C代码中增加 #include &l ...
- 必会SQL笔试题
()表名:购物信息 购物人 商品名称 数量 A 甲 B 乙 C 丙 A 丁 B 丙 …… 给出所有购入商品为两种或两种以上的购物人记录 答:); ()表名:成绩表 姓名 课程 分数 张三 语文 张三 ...
- spring boot系列(五)spring boot 配置spring data jpa (查询方法)
接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...
- DESC加密解密算法
using System; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; usi ...
- 网络实验 04-利用三层交换机实现VLAN间路由
利用三层交换机实现VLAN间路由 一.实验目标 掌握交换机Tag VLAN 的配置 掌握三层交换机基本配置方法 掌握三层交换机VLAN路由的配置方法 通过三层交换机实现VLAN间相互通信 二.实验背景 ...
- CentOS 7 替换网易yum 源
首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...