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...

Longest Substring Without Repeating Characters leetcode

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

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

LeetCode-3. Longest Substring Without Repeating Characters

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

LeetCode 3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating CharactersGiven a string, find the length of the longest subs...

leetcode Longest Substring Without Repeating Characters

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

LeetCode 3 Longest Substring Without Repeating Characters

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

Leetcode - Longest Substring Without Repeating Characters

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

[LeetCode]003-Longest Substring Without Repeating Characters

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

LeetCode-3 Longest Substring Without Repeating Characters

题目:给一个字符串,返回该字符串中最长的不含重复字符的字符串长度 思路:1、暴力搜索,判断每个子字符串是否符合条件    2、定义一个窗口,含左右边界,使用一个map保存窗口中的字符,每次右边界向...

leetcode-Longest Substring Without Repeating Characters

question: my thinking: 首先明确最终要得到最长不重复字符串的长度,就要记录下最长不重复字符串的起始位置和结束位置(当然可能很多朋友有别的更...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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