Longest Substring Without Repeating Characters

来源:互联网 发布:js如何给input赋值 编辑:程序博客网 时间:2024/04/30 08:11

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.

Method:自己写的代码太渣渣了,今天直接上大神的代码吧:https://leetcode.com/discuss/25051/my-o-n-solution-runtime-5ms


int lengthOfLongestSubstring(char *s) {    int m[129] = {0};    int i, j;    int cnt = 0, pre = 0;    int max = 0;    int c;    for (i = 0; c = s[i]; i++) {        if (pre < m[c]) {            if (max < cnt)                max = cnt;            cnt = i-m[c];            pre = m[c];        }        cnt++;        m[c] = i+1;    }    return max > cnt ? max : cnt;}


0 0
原创粉丝点击