3. Longest Substring Without Repeating Characters

来源:互联网 发布:itools怎么下载软件 编辑:程序博客网 时间:2024/05/16 07:42

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 subsequenceand not a substring.

class Solution {public:    int lengthOfLongestSubstring(string s) {         set<char> substring;        int size=s.length();        int *p=new int[size];        int count=0;        for(int i=0;i<size;i++)        {        for(int j=i;j<size;j++)        {        if(substring.find(s[j])!=substring.end())        {        break;        }         else        {                    substring.insert(s[j]);        count++;        }        }        p[i]=count;        substring.clear();        count=0;        }        count=0;        for(int i=0;i<size;i++)        {        if(p[i]>count) count=p[i];        }        return count;    }};


阅读全文
0 0