56. Edit Distance && Simplify Path
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"
- 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的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- Min Edit Distance
Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...
- eclipse调试(debug)的时候,出现Source not found,Edit Source Lookup Path,一闪而过
问题描述 使用Eclipse调试代码的时候,打了断点,经常出现Source not found,网上找了半天,大部分提示点击Edit Source Lookup Path,添加被调试的工程,然而往往没 ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
随机推荐
- 在服务器端将XML转换成HTML
以下是在服务器上转换XML文件所需要的简单源代码: <% 'Load the XML set xml = Server.CreateObject("Microsoft.XMLDOM&q ...
- Read/Write file in Android
http://www.cnblogs.com/freeliver54/archive/2011/09/16/2178910.html
- Java中Stringbuffer、Arrays、Interger、Character类的特性
1:StringBuffer(掌握) (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了 一个字符串缓冲区类.StringBuffer供我们使 ...
- Oracle数据库高效sql语句的整理
业务需求说明:由于之前公司后台APP端有一个document表,该表中包含了所有的信息,新的需求就是通过该表创建出一个新的用户表(usertable)和一个档案表(document,该表只保留原doc ...
- Android Studio Reference local .aar files
repositories { flatDir { dirs 'libs' }} dependencies { compile 'com.android.support:support-v4:22.2. ...
- xpcall 安全调用
-- xpall (调用函数f, 错误函数fe[, 参数]) function fun(a,b) -- 这里的参数没什么实际作用,就是展示下用法 return a / bend -- xpc ...
- Mongodb在NUMA机器上的优化
10gen在mongodb的部署指南上,提到了在NUMA机器上,mongodb可能会出现问题,参见:http://docs.mongodb.org/manual/administration/prod ...
- Socket编程基础——面向连接TCP
WinSock是Windows环境下的网络编程接口,它最初是基于Unix环境下的BSD Socket,是一个与网络协议无关的编程接口.WinSock包含两个主要版本,即WinSock1和WinSock ...
- 如何成为一名优秀的前端工程师 (share)
发现一篇不错的博文,和大家分享一下,为有志成为一名优秀前端工程师的童鞋们提供一个参考. :)~ 本文来源:http://www.biaodianfu.com/what-makes-a-good-fro ...
- H5版俄罗斯方块(1)---需求分析和目标创新
前言: 俄罗斯方块和五子棋一样, 规则简单, 上手容易. 几乎每个开发者, 都会在其青春年华时, 签下"xx到此一游". 犹记得大一老师在布置大程作业的时候提过: "什么 ...