当前位置:   article > 正文

代码随想录算法训练营第55天|583. 两个字符串的删除操作,72. 编辑距离

代码随想录算法训练营第55天|583. 两个字符串的删除操作,72. 编辑距离

583. 两个字符串的删除操作

  1. class Solution {
  2. public:
  3. int minDistance(string word1, string word2) {
  4. int m = word1.size();
  5. int n = word2.size();
  6. vector<vector<int>> dp(m + 1, vector<int>(n + 1));
  7. for (int i = 1; i <= m; ++i) {
  8. dp[i][0] = i;
  9. }
  10. for (int j = 1; j <= n; ++j) {
  11. dp[0][j] = j;
  12. }
  13. for (int i = 1; i <= m; i++) {
  14. char c1 = word1[i - 1];
  15. for (int j = 1; j <= n; j++) {
  16. char c2 = word2[j - 1];
  17. if (c1 == c2) {
  18. dp[i][j] = dp[i - 1][j - 1];
  19. } else {
  20. dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
  21. }
  22. }
  23. }
  24. return dp[m][n];
  25. }
  26. };

java版:

  1. class Solution {
  2. public int minDistance(String word1, String word2) {
  3. int m = word1.length(), n = word2.length();
  4. int[][] dp = new int[m + 1][n + 1];
  5. for (int i = 1; i <= m; i++) {
  6. char c1 = word1.charAt(i - 1);
  7. for (int j = 1; j <= n; j++) {
  8. char c2 = word2.charAt(j - 1);
  9. if (c1 == c2) {
  10. dp[i][j] = dp[i - 1][j - 1] + 1;
  11. } else {
  12. dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
  13. }
  14. }
  15. }
  16. int lcs = dp[m][n];
  17. return m - lcs + n - lcs;
  18. }
  19. }

72. 编辑距离

  1. class Solution {
  2. public:
  3. int minDistance(string word1, string word2) {
  4. int n = word1.length();
  5. int m = word2.length();
  6. if (n * m == 0) return n + m;
  7. vector<vector<int>> D(n + 1, vector<int>(m + 1));
  8. for (int i = 0; i < n + 1; i++) {
  9. D[i][0] = i;
  10. }
  11. for (int j = 0; j < m + 1; j++) {
  12. D[0][j] = j;
  13. }
  14. for (int i = 1; i < n + 1; i++) {
  15. for (int j = 1; j < m + 1; j++) {
  16. int left = D[i - 1][j] + 1;
  17. int down = D[i][j - 1] + 1;
  18. int left_down = D[i - 1][j - 1];
  19. if (word1[i - 1] != word2[j - 1]) left_down += 1;
  20. D[i][j] = min(left, min(down, left_down));
  21. }
  22. }
  23. return D[n][m];
  24. }
  25. };

java版:

  1. class Solution {
  2. public int minDistance(String word1, String word2) {
  3. int n = word1.length();
  4. int m = word2.length();
  5. // 有一个字符串为空串
  6. if (n * m == 0) {
  7. return n + m;
  8. }
  9. // DP 数组
  10. int[][] D = new int[n + 1][m + 1];
  11. // 边界状态初始化
  12. for (int i = 0; i < n + 1; i++) {
  13. D[i][0] = i;
  14. }
  15. for (int j = 0; j < m + 1; j++) {
  16. D[0][j] = j;
  17. }
  18. // 计算所有 DP 值
  19. for (int i = 1; i < n + 1; i++) {
  20. for (int j = 1; j < m + 1; j++) {
  21. int left = D[i - 1][j] + 1;
  22. int down = D[i][j - 1] + 1;
  23. int left_down = D[i - 1][j - 1];
  24. if (word1.charAt(i - 1) != word2.charAt(j - 1)) {
  25. left_down += 1;
  26. }
  27. D[i][j] = Math.min(left, Math.min(down, left_down));
  28. }
  29. }
  30. return D[n][m];
  31. }
  32. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/214809
推荐阅读
相关标签
  

闽ICP备14008679号