4.17 leetcode -17 longest-substring-without-repeating-characters

来源:互联网 发布:windows无法连接到cmcc 编辑:程序博客网 时间:2024/06/11 04:13
题目描述

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.


这个题目就比较有意思了,最小子串,里面不能包含重复的字符。

我觉得用桶(或者map)就能做.

class Solution {public:    int lengthOfLongestSubstring(string s) {        bool btong[255];        for(int i = 0;i < 255;i++)            btong[i] = false;                int nlength = s.length();        int start = 0;        int nmax = 0;        for(int i = 0;i < nlength;i++)            {            if(btong[s[i]] == false)                btong[s[i]] = true;            else                {                if((i-start) > nmax)                    nmax = (i-start);                while(s[start] != s[i])                    {                    btong[s[start]] = false;                    start ++;                }                start ++;            }        }        if((nlength-start) > nmax)           nmax = (nlength-start);       return nmax;    }};


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