LeetCode:M-583. Delete Operation for Two Strings
来源:互联网 发布:mac 终端翻墙 编辑:程序博客网 时间:2024/06/05 04:33
LeetCode链接
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
Example 1:
Input: "sea", "eat"Output: 2Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Note:
- The length of given words won't exceed 500.
- Characters in given words can only be lower-case letters.
class Solution { //求最长公共子序列 public int minDistance(String word1, String word2) { if(word1==null || word2==null) return 0; int n1 = word1.length(); int n2 = word2.length(); if(n1==0 || n2==0) return Math.max(n1,n2); int[][] dp = new int[n1][n2]; for(int i=0; i<n1; i++){ for(int j=0; j<n2; j++){ if(word1.charAt(i)==word2.charAt(j)) dp[i][j] = (i==0||j==0)?1:(dp[i-1][j-1]+1); dp[i][j] = Math.max(dp[i][j], Math.max(i==0?0:dp[i-1][j], j==0?0:dp[i][j-1])); } } return n1+n2-2*dp[n1-1][n2-1]; } }
阅读全文
0 0
- LeetCode:M-583. Delete Operation for Two Strings
- [LeetCode]583. Delete Operation for Two Strings
- [leetcode] 583. Delete Operation for Two Strings
- [leetcode]583. Delete Operation for Two Strings
- leetcode 583. Delete Operation for Two Strings
- [LeetCode] 583. Delete Operation for Two Strings
- [leetcode 583] Delete Operation for Two Strings
- #leetcode编程日记#583. Delete Operation for Two Strings
- 583. Delete Operation for Two Strings Medium
- 583. Delete Operation for Two Strings
- 583. Delete Operation for Two Strings
- 583. Delete Operation for Two Strings
- 583.Delete Operation for Two Strings
- Delete Operation for Two Strings
- Leetcode之Delete Operation for Two Strings 问题
- 【Leetcode-Medium-583】Delete Operation for Two Strings
- leetcode 583. Delete Operation for Two Strings 最长公共子串 + DP动态规划
- leetcode583. Delete Operation for Two Strings
- 自用 VS2010 开发环境编辑风格 / .vssettings文件使用
- readonly 与 disabled的区别
- Python的命令行参数解析
- 企业微信自动打卡插件开发
- 单系统中高并发下单例模式
- LeetCode:M-583. Delete Operation for Two Strings
- 计算机体系结构--海明码
- 布局优化---merge标签的使用
- android 添加一个按键键值
- Python的numpy库中将矩阵转换为列表等函数
- Lcov显示分支覆盖率
- 芯片的设计和生产流程学习笔记
- 计算机体系结构--CRC
- 关于线程池的一段对话