标签:substring leetcode algorithm 面试题
int lengthOfLongestSubstring(string s) {
int len = s.size();
if(len <= 1)
return len;
//hash_table,record the index of items in s
int hash_table[255];
//-1 means haven't occur
memset(hash_table, -1, sizeof(hash_table));
int start = 0;
int end = 0;
int max_len = 0;
while(end < len)
{
char c = s[end];
if(hash_table[c] == -1)
hash_table[c] = end++;
else
{
max_len = max(max_len, end - start);
//new start
start = hash_table[c] + 1;
//clean items in hash_table .
memset(hash_table, -1, sizeof(hash_table));
//record the index of items from new start to end.
for(int i = start; i <= end; ++i)
hash_table[s[i]] = i;
++end;
}
}
//may be the s without repeating character from start to end.
max_len = max(max_len, end - start);
return max_len;
}【leetcode】 Longest Substring Without Repeating Characters,布布扣,bubuko.com
【leetcode】 Longest Substring Without Repeating Characters
标签:substring leetcode algorithm 面试题
原文地址:http://blog.csdn.net/shiquxinkong/article/details/27708651