Leetcode: Longest Substring Without Repeating Characters

原创 2016年08月31日 16:24:25
int lengthOfLongestSubstring(char* s) {
    int n=strlen(s);
    if(n==0){
        return 0;
    }
    int hash[256];
    memset(hash,-1,sizeof(hash));
    int max_=1;   //保存答案
    int i=-1,j=0;//i为当前无重复字符串的起始位置-1
    for(int j=0;j<n;j++){
       if(hash[s[j]]>i){ //如果出现了重复
           i=hash[s[j]]; //改变起始位置
       }
       if(j-i>max_){  //更新max_
           max_=j-i;
       }
       hash[s[j]]=j;   //更新最近的s[j]位置
    }
    return max_;
}

版权声明:本文为博主原创文章,欢迎指教~

相关文章推荐

【LeetCode】Longest Substring Without Repeating Characters

问题描述Given a string, find the length of the longest substring without repeating characters.Examples:G...

[LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters

1 问题 Given a string, find the length of the longest substring without repeating characters. Exa...

Longest Substring Without Repeating Characters-----LeetCode

题目Given a string, find the length of the longest substring without repeating characters.Examples:Giv...

leetcode003 Longest Substring Without Repeating Characters

题目3. Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

leetcode: Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

(leetcode) Longest Substring Without Repeating Characters

Question : Given a string, find the length of the longest substring without repeating characters. ...

LeetCode- 3. Longest Substring Without Repeating Characters

Description Given a string, find the length of the longest substring without repeating characters.E...

leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters标签(空格分隔): leetcode string twoPoints setGi...

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode: Longest Substring Without Repeating Characters
举报原因:
原因补充:

(最多只允许输入30个字)