首页 > 代码库 > Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters
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)解法。
class Solution {private: struct myarray{ bool dp; int loc; };public: int lengthOfLongestSubstring(string s) { if(s.empty()) return 0; struct myarray dps[1000]; for (int i=0; i<1000; ++i) { dps[i].dp=false; dps[i].loc=-1; } int reslen=0; int nextStart=0; int i; for(i=0;i<s.size();++i) { if(dps[s[i]].dp==false) { dps[s[i]].dp=true; dps[s[i]].loc=i; } else { if(i-nextStart>reslen) reslen=i-nextStart; nextStart=dps[s[i]].loc+1; i=nextStart-1; for (int i=0; i<1000; ++i) { dps[i].dp=false; dps[i].loc=-1; } } } if(i==s.size()) if(s.size()-nextStart>reslen) reslen=s.size()-nextStart; return reslen; }};
Longest Substring Without Repeating Characters
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。