zoukankan      html  css  js  c++  java
  • 3. Longest Substring Without Repeating Characters (ASCII码128个,建立哈西表)

    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(char* s) {
        int latestPos[128]; //map<char,int>
        int result = 0;
        int i = 1;
        int startPos = 0;
    
        if(strcmp(s,"")==0) return result;
        
        memset(latestPos, -1, sizeof(latestPos));
        latestPos[s[0]] = 0;
        while(s[i]!=''){
            if(latestPos[s[i]] >= startPos){ //s[i] already appeared
                if(i-startPos > result) result = i-startPos;
                startPos = latestPos[s[i]]+1;
            }
            latestPos[s[i]] = i;
            i++;
        }
        if(i-startPos > result) result = i-startPos;
        return result;
    }
  • 相关阅读:
    k8s-istio记录
    k8s
    单词 -(动物)
    RxJs
    .netcore 3.1 unbuntu
    单词规整
    AutoMapper
    时间
    ye
    特殊权限
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/5271415.html
Copyright © 2011-2022 走看看