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 substring is "b", with the length of 1.

这道题的意思是要找出给定字符串中不包含重复字符的最长子串。

算法不难,这里需要额外的空间记录每个字符是否已经出现过,以及一个头指针p和一个尾指针q。

循环每次向后移q,如果s[q]未出现,则标记为出现,否则在[p,q)范围内找到s[q]第一次出现的位置记为t,将p到t都标记为未出现,再将p更新为t+1,(别忘了标记当前正在处理的字符)直到处理完所有字符串为止。

实际上我在这里走了弯路。第一次通过的代码用时148ms,排在C++分布的后半部分了,代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        string t;
        bool hasAppeared[128];
        int max = 0, _pos;

        if (s.size() == 0 || s.size() == 1)
            return s.size();

        for (int i = 0; i < 128; i++)
            *(hasAppeared + i) = false;

        for (int i = 0; i < s.size(); i++) {
            if (!hasAppeared[s[i]]) {
                hasAppeared[s[i]] = true;
                t += s[i];
            }
            else {
                _pos = t.find(s[i]);
                max = max < t.size() ? t.size() : max;
                for (int j = 0; j <= _pos; j++)
                    hasAppeared[t[j]] = false;
                t = t.substr(_pos + 1);
                t += s[i];
                hasAppeared[s[i]] = true;
            }
        }

        max = max < t.size() ? t.size() : max;

        return max;
    }
};

因为对C++不是很熟悉,所以并不清楚有些API的实现。我猜测,应该是substr方法占用的时间较多。也许是拷贝了一份原字符串。当时还觉得这种写法看起来还比较elegant。

想了半天思路应该是没错的,于是换用了指针(下标),代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        bool hasAppeared[128];
        int i, max = 0, _pos, _st = 0;

        if (s.size() == 0 || s.size() == 1)
            return s.size();

        for (i = 0; i < 128; i++)
            *(hasAppeared + i) = false;

        for (i = 0; i < s.size(); i++) {
            if (!hasAppeared[s[i]]) {
                hasAppeared[s[i]] = true;
            }
            else {
                _pos = s.find(s[i], _st);
                max = max < i - _st ? i - _st : max;
                for (int j = _st; j <= _pos; j++)
                    hasAppeared[s[j]] = false;
                _st = _pos + 1;
                hasAppeared[s[i]] = true;
            }
        }

        max = max < i - _st ? i - _st : max;

        return max;
    }
};

这样运行时间就缩短到了48ms,也基本算是最优了。每次我都很困惑,前面那些运行时间接近0的和快的超过数学最优解的到底是怎么回事。纠结无益,继续学习。

时间: 2024-10-09 12:16:51

LeetCode No.3 Longest Substring Without Repeating Characters的相关文章

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-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】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

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

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 lon

[Leetcode 3, Medium] Longest Substring Without Repeating Characters

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 long

Leetcode OJ #3 Longest Substring Without Repeating Characters

https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: 找一个字符串的连续子串,使得该子串里不含相同字母,求符合条件的最长的子串的长度. 算法: DP----后缀思想 最初考虑过用二分答案,发现复杂度应该是O(n*n*log(n)),其中n*n验证一个答案对不对,log(n)是枚举可能答案的长度.然后就放弃,考虑DP dp[i]:以str[i]结尾的符合条件的子串的最大长度. 那么dp[i

LeetCode题解 #3 Longest Substring Without Repeating Characters

找出字符串中没有相同字符的的最长串 注意这里的 Characters指的是字符,不是字母,就是说|/?~这样的字符都会出现,所以要用到ASCII码 最简单的方法是,从第一个字符开始,往后一个个判断,里面有没有重复的字符,如果重复了则记录下长度. 例如:abcabcbb 第一次:abc 重复于a  长度3 第二次:bca 重复与b  长度3 第三次:cab 重复与c  长度3 ...... 但这种方法很耗时 如果是 abcdefghijk这种 第一次就找到了的 abcdefghijk 但还要第二次

LeetCode:3.Longest Substring Without Repeating Characters

思路:看到题目首先想到最大字符串匹配KMP算法 1 public static int lengthOfLongestSubstring(String s) { 2 int maxLength = 0; 3 StringBuilder sb = new StringBuilder(s); 4 a:for(int i = 0;i<sb.length();i++){ 5 StringBuilder sb2 = new StringBuilder(""); 6 sb2.append(s