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;
    }
  • 相关阅读:
    图书管理系统
    关键路径
    最短路径

    最小生成树、最短路径
    Huffman编码
    LA 3401
    UVA 10881
    OI 刷题记录——每周更新
    4396: [Usaco2015 dec]High Card Wins
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/5271415.html
Copyright © 2011-2022 走看看