题目如下:

解题思路:本题和【leetcode】583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word1[i]还是word2[j]取决于min(dp[i-1][j]+ord(word1[i-1]),dp[i][j-1]+ord(word2[j-1]))。

代码如下:

class Solution(object):
def minimumDeleteSum(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
word1 = s1
word2 = s2
if len(word1) == 0 or len(word2) == 0:
return abs(len(word2) - len(word1))
dp = [[0 for x in range(len(word2)+1)] for x in range(len(word1)+1)]
w1_ascii = 0
w2_ascii = 0
for i in xrange(1,len(word1)+1):
w1_ascii += ord(word1[i-1])
dp[i][0] = w1_ascii
for j in xrange(1,len(word2)+1):
w2_ascii += ord(word2[j-1])
dp[0][j] = w2_ascii
for i in xrange(1,len(word1)+1):
for j in xrange(1,len(word2)+1):
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j]+ord(word1[i-1]),dp[i][j-1]+ord(word2[j-1]))
return dp[-1][-1]

【leetcode】712. Minimum ASCII Delete Sum for Two Strings的更多相关文章

  1. 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. LN : leetcode 712 Minimum ASCII Delete Sum for Two Strings

    lc 712 Minimum ASCII Delete Sum for Two Strings 712 Minimum ASCII Delete Sum for Two Strings Given t ...

  3. LC 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  4. [LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  5. LeetCode 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  6. 712. Minimum ASCII Delete Sum for Two Strings

    题目: Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings eq ...

  7. Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings)

    Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings) 给定两个字符串s1, s2,找到 ...

  8. [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  9. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

随机推荐

  1. SQL优化—nested loop优化

    跑批时间段22:00-23:00,生成AWR报告 分析sql:SQL_ID='5hfw4smzs2pqw' 执行计划: SQL> select *  FROM TABLE(DBMS_XPLAN. ...

  2. ES与CQRS之旅

    引言 领域驱动设计(Domain Driven Design),使用统一的建模语言.专注业务领域分析.采取化整为零并反复迭代的方式,以业务领域模型为圆心,向外辐射到系统轮廓的勾勒.具体模块的实现,为我 ...

  3. bp文件错误消除

    代码文件报错, error: unused parameter 'data' [-Werror,-Wunused-parameter]‘ 按提示在cflags中加入: "-Wunused-p ...

  4. jmeter接口测试初体验

    今天初体验了一把jmeter,把操作的一些经历贴出来,督促自己进步.等逐步掌握后再次回首时,希望是有所思的,欣慰的! jmeter: Apache JMeter是Apache组织开发的基于Java的压 ...

  5. 几个模拟OLE事件注册、调用的宏

    最近遇到一个要求,将原来的OCX控件,替换成直接的DLL调用. 遇到OLE的事件回调,写了三个宏,用于简化代码 #define OLE_ENVENT_IN_CLASS_ONE(event_name, ...

  6. 【ABAP系列】SAP ABAP 用BAPI批量导入物料的质量视图

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 用BAPI批量导入 ...

  7. [USACO 2008 Jan. Silver]架设电话线 —— 最短路+二分

    一道图论的最短路题.一开始连最短路都没想到,可能是做的题太少了吧,完全没有思路. 题目大意: FJ的农场周围分布着N根电话线杆,任意两根电话线杆间都没有电话线相连.一共P对电话线杆间可以拉电话线,第i ...

  8. pandas 入门(2)

    from pandas import Series, DataFrame, Index import numpy as np from numpy import nan as NA obj = Ser ...

  9. vuejs基础-MVVM结构

    Vue.js 基本代码 和 MVVM 之间的对应关系 处理过程: 每当用户进行业务处理时,如果需要进行业务处理,都会通过网络请求,去请求后端的服务器,此时,我们的这个请求,就会被后端的App.js监听 ...

  10. [Web 前端] 006 css 三种页面引入的方法

    1. 外链式 用法 step 1: 在 html 文档的 head 头部分写入下方这句话 <link rel="stylesheet" href="./xxx.cs ...