longest Substring Without Repeating Characters

来源:互联网 发布:手机屏幕镜像软件 编辑:程序博客网 时间:2024/06/08 18:19

题目:

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.


思路:

1)记录每个字符在字符串出现的最新的位置。

2)用一个变量last,表示“子串”开始前的一个位置,最开始该变量为-1。

3)当字符串中的一个字符已经在最新的子串”中出现过时,该子串”到此为止,新的子串开始计数。

此时,需要做两件事情:

a) 更新 last

b) 判断是否更新,最长子串的值

4)结束时,子串计数自动停止,所以要对最后一个子串进行处理

class Solution {public:    int lengthOfLongestSubstring(string s) {        int len=s.size();        if (len<=1)  return len;        int index[256];        int max=0;        int last=-1;        memset(index,-1,sizeof(index));        for(int i=0;i<len;i++)        {            if((index[s[i]]!=-1)&&(last<index[s[i]]))            {                if((i-1-last)>max)                {                    max=i-1-last;                }                last=index[s[i]];               }            /*if((i-last)>max)            {                 max=i-last;            }*/            index[s[i]]=i;        }        if((len-1-last)>max)        {            max=len-1-last;        }        return max;    }};
别人的代码:

// Source : https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/// Author : Hao Chen// Date   : 2014-07-19/********************************************************************************** * * Given a string, find the length of the longest substring without repeating characters. * For example, the longest substring without repeating letters for "abcabcbb" is "abc", * which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.*               **********************************************************************************/#include <string.h>#include <iostream>#include <string>#include <map>using namespace std;/* * Idea: *  * Using a map store each char's index. *  * So, we can be easy to know the when duplication and the previous duplicated char's index. *  * Then we can take out the previous duplicated char, and keep tracking the maxiumn length.  *  */int lengthOfLongestSubstring1(string s) {    map<char, int> m;    int maxLen = 0;    int lastRepeatPos = -1;    for(int i=0; i<s.size(); i++){        if (m.find(s[i])!=m.end() && lastRepeatPos < m[s[i]]) {            lastRepeatPos = m[s[i]];        }        if ( i - lastRepeatPos > maxLen ){            maxLen = i - lastRepeatPos;        }        m[s[i]] = i;    }    return maxLen;}//don't use <map>int lengthOfLongestSubstring(string s) {    const int MAX_CHARS = 256;    int m[MAX_CHARS];    memset(m, -1, sizeof(m));    int maxLen = 0;    int lastRepeatPos = -1;    for(int i=0; i<s.size(); i++){        if (m[s[i]]!=-1 && lastRepeatPos < m[s[i]]) {            lastRepeatPos = m[s[i]];        }        if ( i - lastRepeatPos > maxLen ){            maxLen = i - lastRepeatPos;        }        m[s[i]] = i;    }    return maxLen;}int main(int argc, char** argv){    string s = "abcabcbb";    cout << s << " : " << lengthOfLongestSubstring(s) << endl;    s = "bbbbb";    cout << s << " : " << lengthOfLongestSubstring(s) << endl;    s = "bbabcdb";    cout << s << " : " << lengthOfLongestSubstring(s) << endl;    if (argc>1){        s = argv[1];        cout << s << " : " << lengthOfLongestSubstring(s) << endl;    }    return 0;}



阅读全文
0 0
原创粉丝点击