LeetCode --- Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

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.

My Submitted Code

struct my_hash_node{
    unsigned int 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(unsigned char));
    }
    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_++;
        return 0;
    }
    int find(char c,my_hash_node& node){
        int index=c%256;
        if(hash_index_table_[index]==0){
            return 0;
        }else{
            node=hash_table_[index];
            return 1;
        }
    }
    void clear(){
        memset(hash_table_,0,256*sizeof(my_hash_node));
        memset(hash_index_table_,0,256*sizeof(unsigned char));
        size_=0;
    }
    int size(){
        return size_;
    }
    int erase(char c){
        return 0;
    }
private:
    my_hash_node hash_table_[256];
    unsigned char hash_index_table_[256];
    unsigned int size_;
};

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int maxlen=0;
        int strlen=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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值