Java for LeetCode 072 Edit Distance【HARD】
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
解题思路:
DP问题,参考思路Edit Distance@LeetCode,其中,数组我们可以用一维即可,用一个upleft表示左上的值即(dp[i-1][j-1]),JAVA实现如下:
public int minDistance(String word1, String word2) {
int[] dp = new int[word2.length() + 1];
for (int i = 0; i < dp.length; i++)
dp[i] = i;
for (int i = 1; i <= word1.length(); i++) {
int upleft = dp[0];
dp[0] = i;
for (int j = 1; j <= word2.length(); j++) {
int temp = dp[j];
if (word1.charAt(i - 1) == word2.charAt(j - 1))
dp[j] = upleft;
else
dp[j] = Math.min(upleft, Math.min(dp[j], dp[j - 1])) + 1;
upleft = temp;
}
}
return dp[word2.length()];
}
Java for LeetCode 072 Edit Distance【HARD】的更多相关文章
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 115 Distinct Subsequences【HARD】
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Java for LeetCode 097 Interleaving String 【HARD】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:累加数【306】
LeetCode:累加数[306] 题目描述 累加数是一个字符串,组成它的数字可以形成累加序列. 一个有效的累加序列必须至少包含 3 个数.除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相 ...
- LeetCode:二进制手表【401】
LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...
- LeetCode:翻转二叉树【226】
LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...
- LeetCode:棒球比赛【682】
LeetCode:棒球比赛[682] 题目描述 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. " ...
随机推荐
- python 学习笔记2(list/directory/文件对象/模块/参数传递)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象. 11. list list是一个类.每个列表都属于该类. >>>nl = [1,2,5,3, ...
- 18.Android之SharedPreferences数据存储学习
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- BZOJ1083 繁忙的都市
Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口 ...
- hihocoder 1169 猜数字
传送门 时间限制:10000ms 单点时限:5000ms 内存限制:256MB 描述 你正在和小冰玩一个猜数字的游戏.小冰首先生成一个长为N的整数序列A1, A2, …, AN.在每一轮游戏中,小冰会 ...
- windows搭建openacs编译环境
1.下载ant工具用来编译openacs源码 apache-ant-1.8.2 下载地址http://ant.apache.org/ 这个文件不用编译,在目录bin/下有针对windows的ant 2 ...
- UVA1220Party at Hali-Bula(树的最大独立集 + 唯一性判断)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/H 紫书P282 员工和直属老板只能选一个,最多选多少人 思路 ...
- 关于updateElement接口
1.bool UpdateGameElement(const struct_game_element& ele, gs_dbs_user_info_op_req& db_req, :: ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 织梦(dedecms) 5.7 /plus/car.php sql注入0day
测试方法: @Sebug.net dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! <?php $host=$argv[1]; $path=$argv[2]; $ ...
- systemctl 命令的用法
对比表,以 apache / httpd 为例 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.serv ...