当前位置:网站首页>动态规划(mid)
动态规划(mid)
2022-06-10 10:54:00 【csx_zzh】
剑指 Offer II 096. 字符串交织
class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
if (s1.length() + s2.length() != s3.length()) return false;
int l1=s1.length();
int l2=s2.length();
boolean dp[][]=new boolean[l1+5][l2+5];
dp[0][0]=true;
for (int i = 1; i <= s1.length(); ++ i)
{
//当s2为空时,判断s1能否交织出s3
dp[i][0] = s1.charAt(i-1) == s3.charAt(i-1)&& dp[i - 1][0];
}
for (int j = 1; j <= s2.length(); ++ j)
{
//当s1为空时,判断s2能否交织出s3
dp[0][j] = s2.charAt(j-1) == s3.charAt(j-1) && dp[0][j - 1];
}
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
char ch1 = s1.charAt(i-1), ch2 = s2.charAt(j-1), ch3 = s3.charAt(i+j-1);
dp[i][j]=(ch1==ch3&&dp[i-1][j])||(ch2==ch3&&dp[i][j-1]);
}
}
return dp[l1][l2];
}
}
剑指 Offer II 020. 回文子字符串的个数
class Solution {
public:
int countSubstrings(string s) {
int len = s.length();
int dp[1000][1000];
for(int i=0;i<len;i++){
for(int j=0;j<len;j++) dp[i][j]=false;
}
int res = 0;
for (int i = 0; i < len; i++) {
for (int j = i; j >= 0; j--) {
if (s[i] == s[j]) {
if (i - j <= 1) {
dp[i][j] = true;
} else {
dp[i][j] = dp[i - 1][j + 1];
}
}
res += dp[i][j] ? 1 : 0;
}
}
return res;
}
};
剑指 Offer II 093. 最长斐波那契数列
class Solution {
public int lenLongestFibSubseq(int[] arr) {
int n = arr.length, res = 0;
int[][] dp = new int[n][n];
Map<Integer,Integer>mp=new HashMap<>();
for(int i=0;i<n;i++)
mp.put(arr[i],i);
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
int tmp=arr[i]-arr[j];
// 存在 k 使得 A[i] = A[j] + A[k] (0 <= k < j < i)
if(mp.containsKey(tmp)&&mp.get(tmp)<j){
dp[i][j]=dp[j][mp.get(tmp)]+1;
}
// 存在 k 使得 A[i] = A[j] + A[k] (0 <= k < j < i)
else dp[i][j]=2;
res=Math.max(dp[i][j],res);
}
}
return res>2?res:0;
}
}
边栏推荐
- Switch the Taobao image of NPM
- Uniapp implements authorized login
- [Huang ah code] how to ensure that the images uploaded by PHP are safe?
- Conversion between binary, octal, decimal and hexadecimal (integer plus decimal)
- 杰理之获取 BLE 广播包、profile 数据的接口【篇】
- 【无标题】
- 渡远户外冲刺深交所:年营收3.5亿 林锡臻家族色彩明显
- [high concurrency] about optimistic lock and pessimistic lock, the interviewer of ant financial asked me these questions!!
- 更耐用的游戏真无线耳机,电池超大续航持久,英雄G1上手
- Multithreading implementation
猜你喜欢

VS Code支持配置远程同步了

RT thread usage manual (updated on 2022-0516)

单片机触发器或非门工作原理以及用途

建筑业减碳绝非一招鲜 专家建议加强改造农村建筑

MIT6.824-lab2D-2022(日志压缩详细讲解)

二进制、八进制、十进制、十六进制间互转(整数加小数)

Mit6.824-lab2d-2022 (detailed explanation of log compression)

Noise reduction flagship with excellent sound quality, female poison must be selected, Mo3 experience of shell Prince

Debugging method of cocoslua in vs2013

Development of NFT chain game gamefi system and construction of meta universe games
随机推荐
Vite's public directory
Exception和Error有什么区别吗
[Huang ah code] how to ensure that the images uploaded by PHP are safe?
杰理之BLE功耗异常【篇】
Common shell commands - 02 compression and decompression
string类及学习使用文档
Gan learning notes KL divergence, JS divergence, Wasserstein distance
Mixin -- mixed
MIT6.824-lab2D-2022(日志压缩详细讲解)
PV operation daily question - orange apple question (Preliminary Edition)
fcpx插件:PremiumVFX Animation Presets(动画循环预设) v1.0.1特别版
【BUUCTF】[Zer0pts2020]Can you guess it?
Is there any difference between exception and error
Conversion between binary, octal, decimal and hexadecimal (integer plus decimal)
[high concurrency] about optimistic lock and pessimistic lock, the interviewer of ant financial asked me these questions!!
杰理之BLE 传输速率【篇】
效率工具 : uTools
高性能计算(2)——万丈高楼平地起
珠海高远电能科技有限公司30%股权转让,来自塔米狗分享
Leetcode 2000. Reverse word prefix