LeetCode:Longest Substring Without Repeating Characters

来源:互联网 发布:太祖 知乎 编辑:程序博客网 时间:2024/06/05 17:55

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 hashtable[256];memset(hashtable,-1,sizeof(hashtable));int length=s.length(),cur=0,maxLength=0,curLength=0,index=0;char ch=' ';if(length==0) return 0;if(length==1) return 1;while (index<length){cur=index;while (cur<length){ch=s.at(cur);cur++;if(hashtable[ch]==-1){hashtable[ch]=0;curLength++;}else if(hashtable[ch]==0){if(maxLength<curLength){maxLength=curLength;}curLength=0;memset(hashtable,-1,sizeof(hashtable));break;}}index++;}return maxLength;


 

0 0