Edit Distance

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

思路:动态规划。 
                       D[i+1][j+1] = D[i][j];                                             word1[i] == word2[j],
 D[i+1][j+1] = min(min(D[i][j+1], D[i+1][j]), D[i][j]) + 1;              otherwise.

class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int> > D(word1.size()+1, vector<int>(word2.size()+1, 0));
for(int j = 0; j <= word2.size(); ++j)
D[0][j] = j;
for(int i = 0; i <= word1.size(); ++i)
D[i][0] = i;
for(int i = 0; i < word1.size(); ++i) {
for(int j = 0; j < word2.size(); ++j) {
if(word1[i] == word2[j])
D[i+1][j+1] = D[i][j];
else D[i+1][j+1] = min(min(D[i][j+1], D[i+1][j]), D[i][j]) + 1;
}
}
return D[word1.size()][word2.size()];
}
};

Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:
  • Did you consider the case where path = "/../"? In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/". In this case, you should ignore redundant slashes and return "/home/foo".

注意: /...,    /.home,   /..h2me,   /ho_Me/...   为合法路径。

思路: 从头往尾读: 如是 / 和字符,数字,下划线 , 好判断。若是 '.', 则分情况即可。

inline bool isAlpha(char ch) {
return(('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'));
}
inline bool isAlphaOrUnderline(char ch) {
return isAlpha(ch) || (ch == '_');
}
inline bool isValid(char ch) {
return isAlphaOrUnderline(ch) || ('0' <= ch && ch <= '9');
}
class Solution {// the first alpha should be '/'
public:
string simplifyPath(string path) {
string ans;
path.insert(0, 1, '/'); // but without this state. is OK too.
for(size_t i = 0; i < path.size(); ++i) {
if(path[i] == '.') {
if(i < path.size()-1 && isAlphaOrUnderline(path[i+1])) { ans.push_back('.'); continue;}
else if(i < path.size()-2 && path[i+1] == '.' && (isAlphaOrUnderline(path[i+2]) || path[i+2] == '.')) {
i += 2;
ans.insert(ans.size(), 2, '.');
ans.push_back(path[i]);
continue;
}
}
if(path[i] == '/' && !ans.empty() && ans.back() == '/') continue;
if('0' <= path[i] && path[i] <= '9' && !ans.empty() && ans.back() == '/') continue;
if(path[i] == '/' || isValid(path[i])) ans.push_back(path[i]);
else if(path[i] == '.' && i < path.size()-1 && path[i+1] == '.') {
++i;
if(ans.size() > 1 && ans.back() == '/') ans.pop_back();
while(!ans.empty() && ans.back() != '/') ans.pop_back();
}
}
if(ans.size() > 1 && ans.back() == '/')ans.pop_back();
return ans;
}
};

56. Edit Distance && Simplify Path的更多相关文章

  1. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  2. Min Edit Distance

    Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...

  3. eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过

    问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...

  4. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  5. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  7. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  8. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  9. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

随机推荐

  1. EMS问题

    如果EMS启动后在运行时报出 JMS error: "Not allowed to create destination这个错误,可能就是你启动方式的问题了 进入到EMS的安装目录的bin目 ...

  2. c# 配置文件之configSections配置(二)

    在很多时候我们需要自定义我们自己的自定义App.config 文件,而微软为我们提供了默认的 System.Configuration.DictionarySectionHandler System. ...

  3. jsoup 简介

    Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从HTML中攫取你所需的信 ...

  4. 实现手机扫描二维码页面登录,类似web微信-第一篇,业务分析

    转自:http://www.cnblogs.com/fengyun99/p/3541249.html 关于XMPP组件的文章,先休息两天,好歹已经完整的写了一份. 这两天,先实现一套关于web微信扫描 ...

  5. iOS 视频直播

    ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持 Android 和 iOS, 网上也有很多集成说明, 但是个人觉得还是不够详细, 在这里详细的讲一下在 iOS 中如何集成ijk ...

  6. IOC-AOP

    IOC,依赖倒置的意思,所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如 ...

  7. Sql获取第一天、最后一天

    昨天面试一家公司,上机题目中要求获取每月最后一笔订单.用到了日期的选择性查询,回来在ITeye上找到了这篇文章. 原文: http://new-fighter.iteye.com/blog/17587 ...

  8. RABBITMQ/JAVA (主题)

    上篇博文中,我们进一步改良了日志系统.即使用Direct类型的转换器,使得接受者有能力进行选择性的接收日志,而非fanout那样,只能够无脑的转发. 虽然使用Direct类型的转换器改进了日志系统.但 ...

  9. 三部曲一(数据结构)-1020-Ultra-QuickSort

    通过这道题我大体理解了树状数组的原理和用法,完全用的别人的算法,我把别人算法看懂之后有自己敲了一遍,不得不说这算法真是高深巧妙啊,我开始看都看不懂,还是在别人的讲解下才看懂的,我觉得有必要写个博客记录 ...

  10. C#线性表之顺序表

    线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这种一对一的关系指的是数据元素之间的位置关系,即: ...