[LeetCode]3.LongestSubstringWithoutRepeatingCharacters

3. Longest Substring Without Repeating Characters

我们提供的服务有:成都网站设计、做网站、微信公众号开发、网站优化、网站认证、林口ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的林口网站制作公司

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.

题意:

给定一个字符串,找出最长的无重复的连续字串。就比如"pwwkew"的最长无重复连续字串是"wke"。

最易想到的方法就是字符串逐一开始查找,第一个字符串找到最长的字串,依次找出,取最大值即可。

由于有256个字符,故定义了个257长度的数组,足以存下所有字符。

思路

1)定义字符数组,并把256个的值都置为零。

2)逐个查找字符,如果未出现过,即把该下标对应的数组值置为一。若该下标值已经是一了,则返回,

3)重置全部数组元素元素为零,从下个下标开始继续查找不重复字串。

4)返回最大字串长度即可。

#define CHARACTERS 257

int lengthOfLongestSubstring(char* s) 
{
    if ( !s )
    {   
        return 0;
    }
    
    int character[CHARACTERS] = { 0 };
    int len = strlen(s);
    
    int cnt  = 0;
    int size = 0;
    int maxLen = 0;
    int index = 0;
    for ( index = 0; index < len; index++ )
    {
        size = 0;
        for ( cnt = 0; cnt < CHARACTERS; cnt++ )
        {
            character[cnt] = 0;
        }
        
        for ( cnt = index; cnt < len; cnt++ )
        {
            /* pwwkew */
            int value = *(s + cnt);
            if ( character[value] == 0 )
            {
                size += 1;
                character[value] = 1;
            }
            else
            {
                break;
            }
        }
        
        if ( size > maxLen )
        {
            maxLen = size;
        }
    }
    
    return maxLen;
}

当前题目:[LeetCode]3.LongestSubstringWithoutRepeatingCharacters
标题链接:http://ptjierui.cn/article/pdjgcj.html

其他资讯

Copyright © 2024 青羊区广皓图文设计工作室(个体工商户)(郫都区郫县站) All Rights Reserved 蜀ICP备19037934号
友情链接: 成都网站制作 成都网站建设 成都网站设计 网站建设费用 手机网站设计 高端定制网站设计 营销网站建设 营销型网站建设 成都网站制作 网站制作公司 古蔺网站建设 成都网站制作 成都网站制作 成都网站建设 成都网站建设公司 网站设计公司 成都网站制作 攀枝花网站设计 广安网站设计 企业网站建设 企业网站设计 盐亭网站设计