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. 编辑一个.bat文件来启动一个.erl的程序?

    新建文本输入:"D:\DY\erl\erl5.10.2\bin\erl.exe" -noshell -s convert get_request   保存为xx.bat.. 解释: ...

  2. qsort函数

    qsort函数用法举例 #include <stdio.h> #include <stdlib.h> #include <string.h> //数字比较函数 in ...

  3. Android 微信UI 、点击操作

    上一篇,我们介绍了微信界面的相关知识点.今天我们就来把微信的界面做出来. 首先我们新建一个layout-->LinearLayout-->weixin.xml 我们使用上中下线性布局,采用 ...

  4. Logistic回归小结

    1.梯度上升优化 1). 伪代码: 所有回归系数初始化为1-------------------weights = ones((colNum,1)) 重复r次: 计算整个数据集的梯度gradient ...

  5. 浅谈SEO-收录(二)

    如何更好的让搜索引擎收录网站中的内容,想要被良好的收录,可以尝试一下几点: 一.机器可读 百度通过一个叫做Baiduspider的程序抓取互联网上的网页, 经过处理后建入索引中,目前只支持读文本,fl ...

  6. LPTHW 笨方法学习python 16章

    根据16章的内容作了一些扩展. 比如,判断文件如果存在,就在文件后追加,如不存在则创建. 同时借鉴了shell命令中类似 cat <<EOF > test的方法,提示用户输入一个结尾 ...

  7. I.MX6 eMMC 添加分区

    /********************************************************************************* * I.MX6 eMMC 添加分区 ...

  8. R正则表达式的问题

    今天在处理R的正则表达式的时候发现,R的正则式中的转义字符和linux.python等的还不一样. Linux是使用"\",而R中则使用"[]"! # 我想要将 ...

  9. MetaPhlAn 2:宏基因组进化分析

    描述 MetaPhlAn是分析从物种水平分辨率宏基因组鸟枪法测序数据的微生物群落(细菌,古细菌,真核细胞和病毒)的组成的计算工具.从版本2.0,MetaPhlAn还能够确定具体的菌株(在将样品含有先前 ...

  10. Objective-c——UI基础开发第十天(自动布局)

    一.autoresizing 的使用(了解) 只能参照父控件 1.实现横竖屏幕切换,不能把控件的frame血丝,需要进行屏幕适配 2.需要参照父控件 use auto layout禁用 才会出现aut ...