3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)

题目意思:求字符串中,最长不重复连续子串

思路:使用hashmap,发现unordered_map会比map快,设置一个起始位置,计算长度时,去减起始位置的值

   eg:a,b,c,d,e,c,b,a,e

0 1 2 3 4

0 1 5 3 4

0 6 5 3 4

7 6 5 3 4

7 6 5 3 8

     再次出现c时,将start值置为map[c]+1=3,对于下一个b,因为map[b]<start,则直接map[b]=i

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         unordered_map<char,int> mymap;
 5         int flag=0;
 6         int start=0,i=0;
 7         unordered_map<char,int>::iterator ite;
 8         while(i<s.size()){
 9             ite=mymap.find(s[i]);
10             if(ite==mymap.end()){
11                 mymap[s[i]]=i;
12                 flag=max(flag,i-start+1);
13             }
14             else{
15                 if(mymap[s[i]]>=start)
16                     start=mymap[s[i]]+1;
17                 mymap[s[i]]=i;
18                 flag=max(flag,i-start+1);
19             }
20             ++i;
21         }
22         return flag;
23     }
24 };

时间复杂度:O(n)

超时解法:

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         map<char,int> mymap;
 5         int i=0,max=0,flag;
 6         map<char,int>::iterator ite;
 7         while(i<s.length()){
 8             ite=mymap.find(s[i]);
 9             if(ite==mymap.end()){
10                 mymap[s[i]]=i;
11                 if(mymap.size()>max)
12                     max=mymap.size();
13             }
14             else{
15                 flag=ite->second;
16                 mymap.clear();             //每次清空,是及其失败的做法
17                 for(int j=flag+1;j<=i;++j){
18                     mymap[s[j]]=j;
19                 }
20             }
21             ++i;
22         }
23         return max;
24     }
25 };
时间: 2024-11-06 03:12:57

3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)的相关文章

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 Given a string, find the length of the longest substring without repeating characters. 例子 Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: &q

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

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

[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

【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

Given a string, find the length of the longest substring without repeating characters. Example 1:           Input: "abcabcbb"                              Output: 3                           Explanation: The answer is "abc", with the l

[LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++语言 java语言实现

Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explana

[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

lintcode 中等题:longest substring without repeating characters 最长无重复字符的子串

题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于,"bbbbb",其无重复字符的最长子字符串为"b",长度为1. 解题 利用HashMap,map中不存在就一直加入,存在的时候,找到相同字符的位置,情况map,更改下标 public class Solution { /** * @param s: a s

[LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子串

1.暴力法: 本题让求给定字符串的最长的无重复字符的子串,首先想到暴力解法,穷举出字符串的所有子串,并判断每个子串是否是不重复子串,具体使用hashset或set判是否有重复字符:暴力法效率很差,时间O(n^3),空间O(n);参考代码如下: 1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s){ 4 int res = 0; 5 const int size = s.size(); 6 if(s.empty(

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