【LeetCode with Python】 Edit Distance
来源:互联网 发布:js设置radio不可选 编辑:程序博客网 时间:2024/05/23 11:24
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/edit-distance/
题目类型:动态规划
难度评价:★★★
本文地址:http://blog.csdn.net/nerv3x3/article/details/37334113
b) Delete a character
c) Replace a character
原题页面:https://oj.leetcode.com/problems/edit-distance/
题目类型:动态规划
难度评价:★★★
本文地址:http://blog.csdn.net/nerv3x3/article/details/37334113
Given two words word1 and word2, find the minimum number of steps required to convertword1 toword2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a characterb) Delete a character
c) Replace a character
一道传统意义上的经典的动态规划题目。注意动态规划十有八九可以优化空间复杂度,不是真的要开辟一个矩阵出来。
class Solution: # @return an integer def minDistance(self, word1, word2): if "" == word1 and "" == word2: return 0 elif "" == word1: return len(word2) elif "" == word2: return len(word1) len1 = len(word1) len2 = len(word2) arr1 = [0 for y in range(0, len1 + 1)] arr2 = [0 for y in range(0, len1 + 1)] arr1[0] = 0 for y in range(1, len1 + 1): arr1[y] = y for x in range(1, len2 + 1): arr2[0] = x for y in range(1, len1 + 1): arr2[y] = min(arr1[y - 1] + (0 if (word1[y - 1] == word2[x - 1]) else 1), arr1[y] + 1, arr2[y - 1] + 1) tmp = arr1 arr1 = arr2 arr2 = tmp for y in range(0, len1 + 1): arr2[y] = 0 return arr1[len1]
0 0
- 【LeetCode with Python】 Edit Distance
- 【Leetcode】【python】Edit Distance
- LeetCode: Edit Distance
- LeetCode Edit Distance
- LeetCode: Edit Distance
- [Leetcode] Edit Distance
- leetcode 19: Edit Distance
- [LeetCode] Edit Distance
- [Leetcode] Edit Distance
- LeetCode Edit Distance
- [LeetCode] Edit Distance
- [LeetCode]Edit Distance
- Leetcode: Edit Distance
- [leetcode]Edit Distance
- LeetCode-Edit Distance
- [leetcode] Edit Distance
- LeetCode - Edit Distance
- leetcode之Edit Distance
- APOC-UVA-0003 题解
- CSS基础
- OCP 1Z0 053 91
- oracle sql计划基线
- Lua学习笔记函数
- 【LeetCode with Python】 Edit Distance
- android之VideoView和视频播放View的扩展
- 《老罗Android第二季》ViewPager分页及JSON
- TNS-01106: Message 1106 not found; No message file for product=network, facility=TNS(转)
- map、hashmap
- 闲暇时间看了下DHT网络
- Android Adapter学习之 用法总结
- javascript总结
- 判断二叉树是否平衡以及二叉树的路径和