Created
December 19, 2016 12:30
-
-
Save kirainmoe/a39a65e73efc95012bb7be84d32337c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
inline int lengthOfLongestSubstring(string s) { | |
const int maxs = 96; | |
int pos[maxs]; | |
int ans = 0; | |
int cur = 0; | |
int tmp; | |
int length = s.length(); | |
int flag = length; | |
memset(pos, -1, sizeof(pos)); | |
for (int i = length - 1; i >= 0; i --) | |
{ | |
tmp = s[i] - 32; | |
if (pos[tmp] != -1) | |
{ | |
if (pos[tmp] > flag) { | |
cur = flag - i; | |
ans = max(cur, ans); | |
} else { | |
flag = pos[tmp]; | |
cur = flag - i; | |
} | |
} else { | |
cur += 1; | |
ans = max(cur, ans); | |
} | |
pos[tmp] = i; | |
} | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment