leetcode 编辑距离
class Solution {
public:
int minDistance(string word1, string word2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row=word1.length();
int col=word2.length();
vector< vector<int> > dis(row+1,vector<int>(col+1,0));
dis[0][0]=0;
for(int i=1;i<row+1;i++){
dis[i][0]=i;
}
for(int j=1;j<col+1;j++){
dis[0][j]=j;
}
for(int i=1;i<row+1;i++){
for(int j=1;j<col+1;j++){
int mindis=min(dis[i-1][j]+1,dis[i][j-1]+1);
assert(mindis>=1);
dis[i][j]=min(mindis,dis[i-1][j-1]+getDis(word1[i-1],word2[j-1]));
}
}
return dis[row][col];
}
private:
int getDis(char a,char b){
if(a==b)
return 0;
else
return 1;
}
};
难点:dis数组中row col的下标代表的是string中位置为i-1的字符串
例如:dis【1】【1】为word1【0】到word2【0】的编辑距离。
leetcode 编辑距离的更多相关文章
- LeetCode 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] 161. One Edit Distance 一个编辑距离
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- [LeetCode] 72. 编辑距离 ☆☆☆☆☆(动态规划)
https://leetcode-cn.com/problems/edit-distance/solution/bian-ji-ju-chi-mian-shi-ti-xiang-jie-by-labu ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
- [LeetCode]72. 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示例 1 ...
- [leetcode] 72. 编辑距离(二维动态规划)
72. 编辑距离 再次验证leetcode的评判机有问题啊!同样的代码,第一次提交超时,第二次提交就通过了! 此题用动态规划解决. 这题一开始还真难到我了,琢磨半天没有思路.于是乎去了网上喵了下题解看 ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- new[] class deconstructor
Class class(); Class *class1=new class; class1=&class; delete class1; // Assert 指针指向的是一个栈中的对象, ...
- linux命令:rsync, 同步文件和文件夹的命令
Usage: rsync [OPTION]... SRC [SRC]... DEST or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST or ...
- Oracle语句优化规则(一)
1. 选用适合的ORACLE优化器 ORACLE的优化器共有3种: a. RULE (基于规则) b. COST (基于成本) c. CHOOSE (选择性) 设置缺省的优化 ...
- Yii框架中的CURD操作
<?php $Admin = new Admin(); //查找多条记录,返回二维数组 $Admin->findAll(); $Admin->findAll("id = 2 ...
- Visual Studio shortcut keys
VS2010 快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CT ...
- qstring.h赏析
https://github.com/qtproject/qtbase/blob/dev/src/corelib/tools/qstring.h C:\Qt\Qt5.3.2_min\5.3\mingw ...
- 学校作业-Dp练习
题目 ★Stringsobits01 串 考虑排好序的 N(N<=31)位二进制数. 你会发现,这很有趣.因为他们是排列好的,而且包含所有可能的长度为 N 且含有 1 的个数小于等于 L(L ...
- python手记(41)
python opencv图片融合 #!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 impor ...
- jQuery 之 $(this) 出了什么问题?
近期在写jQuery的时候出了这样一个问题? <html> <head> <title></title> </head> <style ...
- Java开源内容管理CMS系统J4CMS的几个样式
www.j4cms.com java 开源内容管理 页面直接编辑 bootstrap3 样式 default 样式 metro 样式 oschina 样式 leblog 样式 foundation 样 ...