[C++]LeetCode: 105 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 substring is "b", with the length of 1.

思路:我们先来分析下问题,我们需要在一个字符串中找到不重复的最长子字符串。来看张图分析。

由于是字符存储,所以可以用特殊的数组来存储每一个字符在字符串中的位置。这个方法对字符串问题都通用,因为字符本质就是一个Unique的数字,建立一个数组,vector<int> bitmap(256, -1); 数组下标表示这个字符的ASCII码,元素表示这个字符串中的位置。比如,q的ASCII码是113,在字符串中坐标为0,
则存储在bitmap[112] = 0.

解决了查找的问题,现在需要考虑我们如何在遇到重复字符后,怎么操作。当我们遇到和之前重复的字符(前一次出现为Occur1,这一次为Occur 2), 这时我们需要做几件事,首先计算当前维护的不重复字符串的长度 = 当前位置 - start(start表示一个标志位,表示当前不重复的子串的起始位置。),
并且比较后维护一个全局最长的不重复子串长度ret; 第二步我们需要更新下一次扫描起点为刚才重复的字符Occur 1的后一位开始;同时我们还需要清空laststart到new start之间的字符在hash table中的坐标,重置-1. 继续扫描知道再次发现相同的字符,和前面一样的处理,注意全部处理完字符串后,还要判断一下末尾的不重复子串是否是最长【很容易忽略】。

复杂度:最坏情况,遍历两遍字符串,O(N)

Answer 1: 

Attention:

1. 注意最后要判断末尾的不重复的子串是否是最长,因为在循环中不会判断。

//最后一次的子字符串并没有更新到ret中,需要判断
        ret = max(ret, (int)s.size()-start);

2. string.size()类型std::basic_string<char>::size_type
,需要进行强制类型转换。

ret = max(ret, s.size()-start);

Line
28: no matching function for call to ‘max(int&, std::basic_string<char>::size_type)’

3. 用vector<256, -1>来维护一个ASCii查找数组,这个方法很好,也常用,需要记住。

vector<int> bitmap(256, -1);    //s的每一个char都可以表示ASC码,可以用数组模仿ASC码

AC Code:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> bitmap(256, -1);    //s的每一个char都可以表示ASC码,可以用数组模仿ASC码
        int ret = 0;
        int start = 0;
        int laststart = 0;

        for(int i = 0; i < s.size(); i++)
        {
            //说明s[i]在现在维护的子字符串中出现
            if(bitmap[s[i]] != -1)
            {
                //i-start表示现在维护的子字符串的长度,维护一个最长的ret
                ret = max(ret, i-start);
                laststart = start;
                start = bitmap[s[i]] + 1; //新的start从上次重复的字母的下一个坐标开始
                //情况laststart到start之间的字母的坐标
                for(int j = laststart; j < start; j++)
                {
                    bitmap[s[j]] = -1;
                }
            }
            bitmap[s[i]] = i; //如果没有重复,将index添加到bitmap中
        }

        //最后一次的子字符串并没有更新到ret中,需要判断
        ret = max(ret, (int)s.size()-start);
        return ret;
    }
};

Answer 2: 更简练的方法

思路:解题的思路和上面一样,只不过通过巧妙的设计,避免了很多操作。我们维护两个变量,一个是最长子串的长度longest, 一个是当前子串的开始位置坐标m.

  • m = max(charIndex[s[i]]+1, m);  如果这个字符没有出现过,charIndex[s[i]]+1为0,不会影响m的取值;如果出现过,charIndex[s[i]]+1代表上一次出现的字符的位置坐标向后移动一位,更新了m.
    调整了下一次搜索子串的起始位置。这一步就避免了上面解法中将laststart到start重置-1的操作,假如字符串:q p x r j x p... 我们计算到x时,置m 为3,下次查表虽然charIndex[‘p‘]存在并且等于1,但是这个坐标一定会小于charIndex[‘x‘]+1(就是上一步更新的m=3),所以不需要担心后面的重复会影响子串的起始位置m. 也就是说后面出现的位置坐标一定在m之前。m取max则无影响。
  • charIndex[s[i]]
    = i;
    不断更新ASCii的表,其中元素为字符串的坐标(如果有重复,更新为距离当前位置左最近的坐标)。
  • longest
    =max(longest, i-m+1);  
    维护一个最长子串的长度,i是当前位置,m是起始位置。

AC Code:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> charIndex(256, -1);
        int longest = 0;
        int m = 0;

        for(int i = 0; i < s.size(); i++)
        {
            m = max(charIndex[s[i]]+1, m);
            charIndex[s[i]] = i;
            longest =max(longest, i-m+1);
        }

        return longest;
    }
};
时间: 2024-08-18 19:10:06

[C++]LeetCode: 105 Longest Substring Without Repeating Characters的相关文章

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

leetcode -day21 Longest Substring Without Repeating Characters

 1.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

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba&qu

[LeetCode][Python]Longest Substring Without Repeating Characters

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.For example, the longest s

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

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 lengt

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] 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