Longest Substring Without Repeating Characters --leetcode

来源:互联网 发布:建筑渲染软件 编辑:程序博客网 时间:2024/06/10 16:16

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.

接口函数:

int lengthOfLongestSubstring(char* s) {}
我的函数:

int lengthOfLongestSubstring(char* s) {    int x,i=0,j=1,k=0,count=0,ans=0;    char f=0;    x=strlen(s);    if(x==1)              //如果为单字符字串,直接返回字串长度1        return 1;    if(x==0)              //如果为空字符串,直接返回字串长度0        return 0;    if(s[0]!=s[1])        //如果前两个字符不同则count从1开始计数        count=1;    do{                      //j不回头的往前爬行,每前进一步,则检测一次整个字串。//        printf("&%d&\n",j);        for(k=i;k<j;k++){            if(s[k]==s[j]){  //如果遇到重合字符,则尾部i跳转到j前面的重合字符的下一个位置。                f=1;                count=j-k;   //计算此时爬虫字串的长度。                i=++k;//                printf("%d",count);//                printf("[%c] ",s[k]);                break;            }        }        if(f!=1){                        count++;          //如果字串里面没有重合字符         }        else            f=0;             //爬虫字串应遇到相同字符而被截断,被重新定义长度。        if(count>ans){       //选取最大串            ans=count;//            printf("-%d-",count);        }        j++;//        printf("\n");    }while(j!=x);    return ans;}



0 0
原创粉丝点击