Leetcode#3 Longest Substring Without Repeating Characters

来源:互联网 发布:c socket传输json 编辑:程序博客网 时间:2024/04/30 18:07

Difficulty:Medium

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) {     int a[256];       memset(a, -1, sizeof(a));     int ans=0, start=0, end1=0;     for(int i =0; i<s.length();i++){        if(a[s[i]]==-1){            end1++;            ans = ans>(end1-start)?ans : (end1-start);            a[s[i]]=i;        }        else{        if(a[s[i]]>=start)            start=a[s[i]]+1;            end1++;            ans = ans>(end1-start)?ans : (end1-start);            a[s[i]]=i;        }     }           return ans;   }


0 0
原创粉丝点击