leetcode题解日练--2016.10.4

来源:互联网 发布:php 16进制 编辑:程序博客网 时间:2024/05/26 09:54

平常心

今日题目:

1、编辑距离
2、字符串中的最长不重复子串

今日摘录:

去不了的才叫做远方

72. Edit Distance | Difficulty: 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

tag:DP|字符串
题意:求两个字符串的编辑距离

思路:
1、典型的DP思路,关键点在于如何列出DP方程
dp[i][0] = i;
dp[0][j] = j;
dp[i][j] = dp[i - 1][j - 1], if word1[i - 1] = word2[j - 1];
dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1), otherwise.

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

结果:29ms

2、用一维向量来表示

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

结果:19ms

3. Longest Substring Without Repeating Characters ​ ​| ​ ​ ​Difficulty: Medium

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

tag:哈希表|两指针|字符串
题意:找到一个字符串中最长不包含重复字符的子串的长度

思路:
1、用两个指针,一个map用来存储每个字符最后出现的位置。i指针代表字符串的尾部,j指针代表字符串的头部,每次移动i,如果i指向的元素在map中,更新j的位置,如果不在的话放入map中更新最长子串的长度。

class Solution {public:    int lengthOfLongestSubstring(string s) {        map<char,int> map;        int res = 0;        for(int i=0,j=0;i<s.size();++i)        {            if(map.find(s[i])!=map.end())            {                j = max(j,map.find(s[i])->second+1);                map[s[i]] = i;            }            else            {                map.insert(make_pair(s[i],i));            }            res = max(res,i-j+1);        }        return res;    }};

结果:95ms
2、类似的思路,不用map会快很多

class Solution {public:    int lengthOfLongestSubstring(string s) {    vector<int> charIndex(128, -1);    int longest = 0, m = 0;    for (int i = 0; i < s.length(); i++) {        m = max(charIndex[s[i]] + 1, m);            charIndex[s[i]] = i;        longest = max(longest, i - m + 1);    }    return longest;}};

结果:26ms

0 0
原创粉丝点击