LeetCode 3: Longest Substring Without Repeating Characters
2013-08-27 20:00
459 查看
Difficulty: 3
Frequency: 2
Problem:
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.
Solution:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i_max_length = 0, i_max_length_from_end = 0;
int p_last_index[26];
memset(p_last_index, 0xFF, sizeof(int)*26);
for (int i = 0; i<s.size(); i++)
{
if (p_last_index[s[i]-'a']==-1||(i-p_last_index[s[i]-'a'])>i_max_length_from_end)
{
p_last_index[s[i]-'a'] = i;
i_max_length_from_end++;
i_max_length = i_max_length>i_max_length_from_end?i_max_length:i_max_length_from_end;
}
else
{
i_max_length_from_end = i - p_last_index[s[i]-'a'];
p_last_index[s[i]-'a'] = i;
}
}
return i_max_length;
}
};
Notes:
Time complexity O(n).
Frequency: 2
Problem:
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.
Solution:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i_max_length = 0, i_max_length_from_end = 0;
int p_last_index[26];
memset(p_last_index, 0xFF, sizeof(int)*26);
for (int i = 0; i<s.size(); i++)
{
if (p_last_index[s[i]-'a']==-1||(i-p_last_index[s[i]-'a'])>i_max_length_from_end)
{
p_last_index[s[i]-'a'] = i;
i_max_length_from_end++;
i_max_length = i_max_length>i_max_length_from_end?i_max_length:i_max_length_from_end;
}
else
{
i_max_length_from_end = i - p_last_index[s[i]-'a'];
p_last_index[s[i]-'a'] = i;
}
}
return i_max_length;
}
};
Notes:
Time complexity O(n).
相关文章推荐
- LeetCode (3)Longest Substring Without Repeating Characters
- leetcode_[Python/C++]_3_Longest Substring Without Repeating Characters(不重复子串最大长度)
- leetcode3-Longest Substring Without Repeating Characters
- LeetCode(3)--Longest Substring Without Repeating Characters
- LeetCode | Longest Substring Without Repeating Characters
- leetcode: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters 无重复字符的最长子串
- leetcode:Longest Substring Without Repeating Characters (双指针扫描) 【面试算法题】
- LeetCode 3 Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- LeetCode 第三题,Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
- LeetCode:Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- leetcode---Longest Substring Without Repeating Characters