Given a string, find thelengthofthe longest substring without repeating characters. For example, the longest substring without repeating letters for"abcabcbb"is"abc", which thelengthis3. For "bbbbb"the longest substring is"b", withthelengthof1.
My Submitted Code
struct my_hash_node{
unsignedint index;
char value;
};
class my_hash{
public:
my_hash():size_(0){
memset(hash_table_,0,256*sizeof(my_hash_node));
memset(hash_index_table_,0,256*sizeof(unsignedchar));
}
int insert(char c,int index){
int table_index=c%256;
my_hash_node node;
node.value=c;
node.index=index;
hash_table_[table_index]=node;
hash_index_table_[table_index]=1;
size_++;
return0;
}
int find(char c,my_hash_node& node){
int index=c%256;
if(hash_index_table_[index]==0){
return0;
}else{
node=hash_table_[index];
return1;
}
}
void clear(){
memset(hash_table_,0,256*sizeof(my_hash_node));
memset(hash_index_table_,0,256*sizeof(unsignedchar));
size_=0;
}
int size(){
return size_;
}
int erase(char c){
return0;
}
private:
my_hash_node hash_table_[256];
unsignedchar hash_index_table_[256];
unsignedint size_;
};
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int maxlen=0;
intstrlen=s.length();
my_hash hashtab;
int tmplen=0;
int repindex=0;
my_hash_node currnode;
for (int i=0;i<strlen;i++)
{
if(hashtab.find(s.at(i),currnode)==0){
hashtab.insert(s.at(i),i);
}else{
tmplen=hashtab.size();
maxlen=(maxlen<tmplen? tmplen:maxlen);
repindex=currnode.index;
hashtab.clear();
i=repindex;
}
}
tmplen=hashtab.size();
if (tmplen>maxlen)
{
maxlen=tmplen;
}
return maxlen;
}
};