Longest Substring Without Repeating Characters - 模拟与哈希

题意很简单,就是寻找一个字符串中连续的最长包含不同字母的子串。

其实用最朴素的方法,从当前字符开始寻找,找到以当前字符开头的最长子串。这个方法猛一看是个n方的算法,但是要注意到由于字符数目的限制,其实这是个O(Cn)的算法,最长也不过是C长度。所以我觉得普通方法应该是能过的。

于是写了一个,字符数目最大也不超过256所以代码如下:

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4
 5         int res=0;
 6         for(int i=0;i<s.length();i++)
 7         {
 8             int flag[200];
 9             memset(flag,0,sizeof(flag));
10             int count = 0;
11             flag[s[i]-‘ ‘] = 1;
12             int tempres = 1;
13             for (int j = i + 1; j < s.length(); j++) {
14                 if (flag[s[j]-‘ ‘]==0) {
15                     flag[s[j]-‘ ‘]= 1;
16                     tempres++;
17                 } else
18                     break;
19             }
20             if (tempres > res)
21                 res=tempres;
22         }
23         return res;
24     }
25 };

其中空格是ASCII码第一个实际意义字符,所以减去‘ ’

正常的O(n)方法是使用哈希表来存已经出现的字符,使用一个指针依次检索,如果碰到已经存在的字符,则去使用第二个指针更新这个哈希表。

时间: 2024-08-27 11:51:57

Longest Substring Without Repeating Characters - 模拟与哈希的相关文章

leetcode4 ---Longest Substring Without Repeating Characters

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 subst

LeetCode: Longest Substring Without Repeating Characters 题解

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 subst

LeetCode3 Longest Substring Without Repeating Characters

题目: 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 s

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

Leetcode 03 Longest Substring Without Repeating Characters

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 subst

Java for LeetCode 003 Longest Substring Without Repeating Characters

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 subst

LeetCode Longest Substring Without Repeating Characters 最长不重复子串

题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离是多少,若大于max,则更新max.若小于,则不更新.每扫到一个字符就需要更新他的出现位置了.这里边还有个注意点,举例说明: 假如有长为16串 s="arbtbqwecpoiuyca" 当扫到第2个b时,距离上一个b的距离是2:(直接减) 当扫到第2个c时,距离上一个c的距离是6:(直接减

LeetCode No.3 Longest Substring Without Repeating Characters

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 subst