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.
Example 1:
Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.
Example 2:
Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum. Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.
Note:
0 < s1.length, s2.length <= 1000.- All elements of each string will have an ASCII value in
[97, 122].
两个字符串最长子序列。
Runtime: 12 ms, faster than 76.47% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int dp[s1.size()+][s2.size()+];
memset(dp, , sizeof(dp));
int sum = ;
for(int i=; i<s1.size(); i++){
sum += (int)s1[i];
for(int j=; j<s2.size(); j++){
if(s1[i] == s2[j]) {
dp[i+][j+] = dp[i][j] + (int)s1[i];
}else {
dp[i+][j+] = max(dp[i+][j], dp[i][j+]);
}
}
}
for(int i=; i<s2.size(); i++){
sum += (int)s2[i];
}
return sum - *dp[s1.size()][s2.size()];
}
};
LC 712. Minimum ASCII Delete Sum for Two Strings的更多相关文章
- 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 ...
- [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. ...
- 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 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 ...
- 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. ...
- Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings)
Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings) 给定两个字符串s1, s2,找到 ...
- [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. ...
- [Swift]LeetCode712. 两个字符串的最小ASCII删除和 | 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. ...
随机推荐
- 【大数据】初识Hadoop
因为项目日志体量较大,每天有4-7T的日志量,传统的sqlserver已经不能满足,所以现在需要使用到大数据的相关工具进行记录和使用. 虽然公共项目提供了组件和解决方案,但是对于一些名词.概念还是有必 ...
- Java并发与多线程
1. 并发与并行 并发是指某个时间段内,多任务交替处理的能力:并行是指同时处理多任务的能力,多核CPU可以实现并行任务. 并发执行的特点: (1)并发程序间相互制约:程序执行结果的相互依赖以及共享资源 ...
- deep_learning_Function_sklearn.preprocessing.LabelBinarizer()
在多数的机器学习比赛中,给出的标签都是非数字化的,所以我们需要对其进行转换.代码如下: from sklearn import preprocessing feature = [[0,1], [1,1 ...
- 对路径“xxxxx”的访问被拒绝。
对路径“D:\\Weixin\\WechatWeb\\wapMxApi\\JsonFile\\WaterPrice.json”的访问被拒绝. 本地vs2013编译调试是没有问题的但是发布后就不能倍访问 ...
- Ubuntu 18.04 手动升级内核
一般情况下,系统正常更新,会自动升级内核到可用的最新版. 查看已安装的内核 $ sudo dpkg -l | grep linux-image 查看当前使用的内核 $ sudo uname -r 查看 ...
- 基于Hexo的个人博客搭建(下)
5.服务器端测试 —5.1 clone到/var/www/html git clone /home/git/repos/myblog.git /var/www/html chown -R git:g ...
- vue项目搭建和开发流程 vue项目配置ElementUI、jQuery和Bootstrap环境
目录 一.VUE项目的搭建 1. 环境搭建 2. 项目的创建和启动 二. 开发项目 1. 配置vue项目启动功能 2. 开发vue项目 (1)项目文件的作用 (2)vue项目开发流程 (3)vue项目 ...
- vue组件,vue补充和总结,JS循环遍历和加减运算、类型转换补充
目录 一.vue中的组件 1. 组件的概念 2. 组件分类 3. 组件的特点 4. 组件的定义 5. 组件化 (1)用法和注意 (2)数据组件化实例 6. 组件传参--父传子 (1)用法和注意 (2) ...
- hivesql-一个表中的数据不在另一个表中
如何最有效的判断 一个表中的数据不在另一个表中 两个方法一个是join 另一个是 exist 方法
- es6 模块编译 *** is not function
今天学习vuejs,里面用到了es6的写法,遇到了一个很怪的问题,不知道有人遇到么. 安装的模块引用:import Vue from 'vue';(注意,Vue处没有{},如果加上这个就报错Uncau ...