3. Longest Substring Without Repeating Characters

来源:互联网 发布:orange pi淘宝 编辑:程序博客网 时间:2024/06/14 19:53

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.

Subscribe to see which companies asked this question.
题意:就是给你一个字符串,找出最长不相同的字符串,例如:pwwkew的结果就是3(wke)
c++方法:
代码:

class Solution {public:    int lengthOfLongestSubstring(string s) {        int len = s.length();        int maxx = 0,ans;        int arr[400];        bool flag = 0;        for(int i=0;i<len;i++)        {            ans = 1,flag = 0;            memset(arr,0,sizeof(int)*400);            arr[s[i]]++;            for(int j=i+1;j<len;j++)            {                arr[s[j]]++;                if(arr[s[j]]>1)                    break;                if(s[i] != s[j])                    ans++;                else                    break;            }            if(ans>maxx)                maxx = ans;        }        return maxx;    }};
0 0