标签:style blog http io ar color os 使用 sp
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.
Hash Table Two Pointers String
算法实现:
我写的代码:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Solution { 6 public: 7 int lengthOfLongestSubstring(string s) { 8 int n = s.length(); 9 if(n<2) return n ; 10 int lft = 0,rgt =0,maxlen = 0; 11 bool sign[128] = {false}; 12 while(rgt<n){ 13 // cout<<lft<<" "<<rgt<<" "<<maxlen<<endl; 14 if(sign[(int)s[rgt]]==false){ 15 sign[(int)s[rgt]]=true; 16 rgt++; 17 if(maxlen<rgt-lft) maxlen = rgt - lft; 18 continue; 19 } 20 sign[(int)s[lft]] = false; 21 lft++; 22 } 23 return maxlen; 24 } 25 }; 26 27 int main() 28 { 29 string s= "242522f23r23rt432twrfs122"; 30 Solution sol; 31 cout<<sol.lengthOfLongestSubstring(s)<<endl; 32 return 0; 33 }
[LeetCode] Longest Substring Without Repeating Characters最长无重复子串
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/Azhu/p/4132670.html