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. 网站后台调用winform MessageLoopApartment

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. HBase with MapReduce (Read and Write)

    上面一篇文章仅仅是介绍如何通过mapReduce来对HBase进行读的过程,下面将要介绍的是利用mapreduce进行读写的过程,前面我们已经知道map实际上是读过程,reduce是写的过程,然而ma ...

  3. css3 弹框提示样式

    .common-dialog-box{ opacity: 0; filter: alpha(opacity=0); position: fixed; top: 0%; left: 50%; z-ind ...

  4. node.js学习笔记【1】

    http://howtonode.org/how-to-install-nodejs http://www.mcclean-cooper.com/valentino/cygwin_install/

  5. python模块之time

    Python中的时间模块. 1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 2.UTC(Coordinated U ...

  6. ACM感悟

    声明:本文是写给弱校ACM新手的一点总结,受自身水平和眼界所限,难免会有一些个人主观色彩,希望大牛指正 感谢@Wackysoft .@哇晴天 . @ 一切皆有可能1 的指教,现根据这些建议,文章已进行 ...

  7. in_array 查询数组中是否存在某个值

    (PHP 4, PHP 5) in_array — 检查数组中是否存在某个值 说明 bool in_array ( mixed $needle , array $haystack [, bool $s ...

  8. js便签笔记(3)——切记:appendChild()、insertBefore()是移动element节点!

    appendChild().insertBefore()是移动element节点,看书的时候注意过,也可以做一个简单的例子测试一下: <div id="div1"> & ...

  9. mac地址泛洪攻击的实验报告

    案例介绍: PC A 访问 本网络的一台FTPserver主机,中间人进行arp的投毒,获取PC-A和FTPserve之间的回话记录,截获用户名和密码. 实验拓扑:

  10. WOJ -1204

    WOJ -1204 1 出现次数大于一半 那么就利用普通的堆栈的思想,如果删除两个不同的元素,原来的多数元素还是多数元素,所以采取按条件入栈的方法,如果和top元素相同则入栈,否则top--,此元素也 ...