Leetcode: 3. Longest Substring Without Repeating Characters

来源:互联网 发布:ubuntu软件中心下载 编辑:程序博客网 时间:2024/05/22 01:35

Leetcode: 3. Longest Substring Without Repeating Characters

一道一般的字符串问题,要求在给定字符串中找出不含重复字符的最长子串的长度。

思路

一个简单的思路是碰到重复的就回到上一个重复的字母的下一位开始找。

    int lengthOfLongestSubstring(string s) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int count[26];        if(s.size() ==0) return 0;        memset(count,-1, sizeof(count));        int start = 0;        int maxV = 0;        for(int i=0; i< s.size(); i++)        {          int index = s[i] - 97;          if(count[index] >= 0)          {                      if(maxV < (i -start))            {              maxV = i-start;            }            i = count[index];            start = i+1;            memset(count,-1, sizeof(count));            continue;          }          count[index] = i;        }        if(maxV < (s.size() -start))        {          maxV = s.size()-start;        }        return maxV;      }  
阅读全文
0 0
原创粉丝点击