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

Longest Substring Without Repeating Characters

时间:2015-03-15 13:43:07      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

首先想到的枚举每个子串。取长度最大的那个,时间复杂度O(N²)

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         if(s.size() == 1)
 5             return 1;
 6         int len = 0;
 7         int maxlen = 0;
 8         for(int i=0;i<s.size();++i)
 9             for(int j=i+1;j<s.size();++j)
10             {
11                 if(s[i]==s[j])
12                 {
13                     string tmp(1,s[i]);
14                     len = 1;
15                     while(j+len < s.size() && s[i+len] == s[j+len])
16                     {
17                         if(string::npos == tmp.find(s[i+len])){
18                             tmp += s[i+len];
19                             ++len;
20                         }
21                     }
22                     if(len>maxlen)
23                         maxlen = len;
24                 }
25             }
26         return maxlen;
27     }
28 };

 

Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/ittinybird/p/4339070.html

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