From c681ec887a1f4d4fb0efdd5c9acde33e23582978 Mon Sep 17 00:00:00 2001 From: Yang Wang Date: Fri, 20 Dec 2019 17:00:44 +0800 Subject: [PATCH] =?UTF-8?q?Week8=20=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Week8作业 --- Week 08/id_546/LeetCode_1143_546.cs | 37 +++++++++++++++ Week 08/id_546/LeetCode_5_546.cs | 36 +++++++++++++++ Week 08/id_546/LeetCode_72_546.cs | 70 +++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 Week 08/id_546/LeetCode_1143_546.cs create mode 100644 Week 08/id_546/LeetCode_5_546.cs create mode 100644 Week 08/id_546/LeetCode_72_546.cs diff --git a/Week 08/id_546/LeetCode_1143_546.cs b/Week 08/id_546/LeetCode_1143_546.cs new file mode 100644 index 000000000..2fad87c3f --- /dev/null +++ b/Week 08/id_546/LeetCode_1143_546.cs @@ -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]; + + + } + } +} diff --git a/Week 08/id_546/LeetCode_5_546.cs b/Week 08/id_546/LeetCode_5_546.cs new file mode 100644 index 000000000..64a145da9 --- /dev/null +++ b/Week 08/id_546/LeetCode_5_546.cs @@ -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; + + + + + + + } + } +} diff --git a/Week 08/id_546/LeetCode_72_546.cs b/Week 08/id_546/LeetCode_72_546.cs new file mode 100644 index 000000000..da4413e42 --- /dev/null +++ b/Week 08/id_546/LeetCode_72_546.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hard +{ + /// + /// 72. 编辑距离 + /// + 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]; + } + + } +}