Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions 37 Week 08/id_546/LeetCode_1143_546.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Medium
{
public class LongestCommonSubsequence
{
public int LongestCommonSubsequence1(string text1, string text2)
{
int[,] dp = new int[text1.Length + 1, text2.Length + 1];


for (int i = 1; i < text1.Length + 1; i++)
{
for (int j = 1; j < text2.Length + 1; j++)
{
//如果末端相同
if (text1[i - 1] == text2[j - 1])
{
dp[i,j] = dp[i - 1,j - 1] + 1;
}
else
{
//如果末端不同
dp[i,j] = Math.Max(dp[i - 1,j], dp[i,j - 1]);
}
}
}
return dp[text1.Length,text2.Length];


}
}
}
36 changes: 36 additions & 0 deletions 36 Week 08/id_546/LeetCode_5_546.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Medium
{
public class LongestPalindromicSubstring
{
public string LongestPalindrome(string s)
{
int n = s.Length;
string res = "";
bool[,] dp = new bool[n,n];
for (int i = n - 1; i >= 0; i--)
{
for (int j = i; j < n; j++)
{
dp[i,j] = s[i] == s[j] && (j - i < 2 || dp[i + 1,j - 1]); //j - i 代表长度减去 1
if (dp[i,j] && j - i + 1 > res.Length)
{
res = s.Substring(i, j + 1-i);
}
}
}
return res;






}
}
}
70 changes: 70 additions & 0 deletions 70 Week 08/id_546/LeetCode_72_546.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hard
{
/// <summary>
/// 72. 编辑距离
/// </summary>
public class EditDistance
{
//推荐解法
public int MinDistance(string word1, string word2)
{
int m = word1.Length;
int n = word2.Length;

int[,] cost = new int[m + 1, n + 1];
for (int i = 0; i <= m; i++)
cost[i, 0] = i;
for (int i = 1; i <= n; i++)
cost[0, i] = i;

for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (word1[i] == word2[j])
cost[i + 1, j + 1] = cost[i, j];
else
{
int a = cost[i, j];
int b = cost[i, j + 1];
int c = cost[i + 1, j];
cost[i + 1, j + 1] = a < b ? (a < c ? a : c) : (b < c ? b : c);
cost[i + 1, j + 1]++;
}
}
}
return cost[m, n];
}


public int minDistance(String word1, String word2)
{
int n1 = word1.Length;
int n2 = word2.Length;
int[,] dp = new int[n1 + 1, n2 + 1];


// 第一行
for (int j = 1; j <= n2; j++) dp[0, j] = dp[0, j - 1] + 1;
// 第一列
for (int i = 1; i <= n1; i++) dp[i, 0] = dp[i - 1, 0] + 1;

for (int i = 1; i <= n1; i++)
{
for (int j = 1; j <= n2; j++)
{
if (word1[i - 1] == word2[j - 1]) dp[i, j] = dp[i - 1, j - 1];
else dp[i, j] = Math.Min(Math.Min(dp[i - 1, j - 1], dp[i, j - 1]), dp[i - 1, j]) + 1;
}
}
return dp[n1, n2];
}

}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.