leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)
2014-06-06 21:03
483 查看
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.
为了提升时间效率,我开辟了一个长度为256的整型数组来标识子串中的字符是否重复,避免了采用循环比较的方法来判定字符是否重复。需要注意的是当我们发现一个字符与当前子串中的字符重复时,此时新的字串需要从子串中第一次出现此字符位置的后一个字符开始作为新的子串的起点,代码中采用了一个整型变量来记录判定过程中的最大子串长度。代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s)
{
int length = s.length(),maxValue=0,count=0;
int flag[256];
Reset(flag);
for(int i=0; i<length; i++)
{
if(flag[s[i]] == -1)
{
count++;
flag[s[i]] = i;
}
else
{
i = flag[s[i]]+1;
Reset(flag);
flag[s[i]] = i;
count=1;
}
if(maxValue < count)
maxValue = count;
}
return maxValue;
}
void Reset(int *flag)
{
for(int i = 0; i<256; i++)
flag[i] = -1;
}
};
repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
为了提升时间效率,我开辟了一个长度为256的整型数组来标识子串中的字符是否重复,避免了采用循环比较的方法来判定字符是否重复。需要注意的是当我们发现一个字符与当前子串中的字符重复时,此时新的字串需要从子串中第一次出现此字符位置的后一个字符开始作为新的子串的起点,代码中采用了一个整型变量来记录判定过程中的最大子串长度。代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s)
{
int length = s.length(),maxValue=0,count=0;
int flag[256];
Reset(flag);
for(int i=0; i<length; i++)
{
if(flag[s[i]] == -1)
{
count++;
flag[s[i]] = i;
}
else
{
i = flag[s[i]]+1;
Reset(flag);
flag[s[i]] = i;
count=1;
}
if(maxValue < count)
maxValue = count;
}
return maxValue;
}
void Reset(int *flag)
{
for(int i = 0; i<256; i++)
flag[i] = -1;
}
};
相关文章推荐
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
- leetcode-java.T003_LongestSubstringWithoutRepeatingCharacters 给定一个字符串,找字符中的最大非重复子串
- Longest Substring Without Repeating Characters计算字符串中不含有重复字符的连续字符串的最大长度
- leetcode_[Python/C++]_3_Longest Substring Without Repeating Characters(不重复子串最大长度)
- 字符的最长无重复子串长度Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters (返回最长无字符重复的子串长度)
- Longest Substring Without Repeating Characters,求没有重复字符的最长字串
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode OJ Longest Substring Without Repeating Characters 不重复的最长字串 滑动窗口
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- LeetCode题库解答与分析——#3.无重复字符的最长子串LongestSubstringWithoutRepeatingCharacters
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- leetcode Longest Substring Without Repeating Characters 没有重复字符的最长子字符串
- [LeetCode] Longest Substring Without Repeating Characters 无重复字符的最长子串
- Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode
- 3. Longest Substring Without Repeating Characters(计算不含重复字符的最长子串的长度)
- Longest Substring Without Repeating Characters 字符串中最长的无重复子串长度
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- Longest Substring Without Repeating Characters求最长子串中没有重复字母的最长长度
- [C++]LeetCode 3: Longest Substring Without Repeating Characters(最长不重复子串)