Leetcode_LongestSubstringWithoutRepeatingCharacters

来源:互联网 发布:无法连接所有网络驱动 编辑:程序博客网 时间:2024/06/05 03:46

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.

解题方法.

该题比较简答,可以用两个指针,也可以用简单的动态规划。注意字符串操作即可。

int lengthOfLongestSubstring(string s) {        if(s.size() == 0)return 0;        int *array = new int[s.size()];        for(int i = 0; i < s.size(); ++i){            array[i] = 0;        }        int currentmax = 1;        array[0] = 1;        for(int i = 1; i < s.size(); ++i){            string substr = s.substr(i-array[i-1], array[i-1]);            size_t found = substr.find(s[i]);            if(found != string::npos){                array[i] = substr.size() - substr.find(s[i]) ;            }            else{                array[i] = array[i-1] + 1;            }            if(array[i] > currentmax){                currentmax = array[i];            }        }        return currentmax;    }


0 0
原创粉丝点击