标签:
设定head和tail两个指针,tail每往前移动一格,便检查tail和head之间的所有字母是否与tail指向的字母重复,如果重复则将head指向重复的后一格
例如:
abcdefdc,当前head指向a,tail指向f,当tail指向下一个d的时候扫描head和tail之间的值,发现c后面的d和tail指向的d重复了,则将head调整为指向e。
class Solution {public:int lengthOfLongestSubstring(string s) {if (s == "")return 0;if (s.size() == 1){return 1;}int head = 0, tail = 1, length = 1;;for (; tail < s.size(); tail++){for (size_t j = head; j < tail; j++){if (s[j] == s[tail]){head = j + 1;break;}}length = (tail - head+1)>length ? (tail - head+1) : length;}return length;}};
Longest Substring Without Repeating Characters
标签:
原文地址:http://www.cnblogs.com/flyjameschen/p/4377499.html