码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Substring Without Repeating Characters

时间:2015-04-18 23:29:41      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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.

https://leetcode.com/problems/longest-substring-without-repeating-characters/

int lengthOfLongestSubstring(char* s) {
    int prev[256];
    int i,j;
    for(i = 0; i < 256; ++i){
        prev[i] = -1;
    }
    int curr_max = 0;
    i = j = 0;
    int len = strlen(s);
    for(; j < len; ++j){
        i = j-i >= j- prev[s[j]] ? prev[s[j]]+1:i;
        curr_max = curr_max < j - i +1 ? j- i+1: curr_max;
        prev[s[j]] = j;
    }
    return curr_max;
}

 

Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/jimmysue/p/4438169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!