码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Longest Substring Without Repeating Characters

时间:2014-09-18 14:40:43      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   div   sp   log   

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

 

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         int asc[180]={-1},max=0,n=0,i=0,j;
 5         for(j=0;j<180;j++)
 6                 asc[j]=-1;
 7         while(i<s.size())
 8         {
 9             if(asc[s[i]]==-1)
10             {
11                 asc[s[i]]=i;
12                 n++;
13                 if(n>max)
14                     max=n;
15             }
16             else
17             {
18                 
19                 int k=asc[s[i]]; 
20                 n=i-k;
21                 for(j=0;j<180;j++)
22                 {
23                     if(asc[j]>-1&&asc[j]<k)
24                         asc[j]=-1;
25                 }
26                 asc[s[i]]=i;
27             }
28             i++;
29         }
30         return max;
31     }
32 };

 

LeetCode:Longest Substring Without Repeating Characters

标签:style   blog   color   io   ar   for   div   sp   log   

原文地址:http://www.cnblogs.com/levicode/p/3979081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!