转载:Longest Substring Without Repeating Characters

来源:互联网 发布:北京java程序员工资 编辑:程序博客网 时间:2024/06/05 21:08

leetcode 最大子串:
问题描述:
Given a string, find the length of the longest substring without repeating characters.

Examples:
Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.


C语言实现:

#include <stdio.h>#include <stdlib.h>#include <string.h>int lengthOfLongestSubstring(char* s) {    int i = 0, j = 0;    int current = 0;    int maxlength = 0;    int start = 0;    int map[128];    memset(map, 0, sizeof(map));    for ( i = 0; map[i]!='\0';  i++)    {        if (++map[s[i]] == 2)        {            if (maxlength < current)            {                maxlength = current;            }            for (j = start; j < i; j++)            {                if (s[j] == s[i])                {                    map[s[j]] = 1;                    start = j + 1;                    break;                }                else                {                    current--;                    map[s[j]] = 0;                }            }        }        else        {            ++current;        }    }    if (maxlength < current)    {        maxlength = current;    }    return maxlength;}

参考:
http://www.cnblogs.com/jiasheng/p/5623066.html

阅读全文
0 0
原创粉丝点击