Longest Substring Without Repeating Characters (c#)

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.

这个问题仅理解他的意思,我就花了很久。。。智商捉急啊

给定一个字符串,求出其中有不重复字符的最长子串。

我一开始的想法是每次遇到有重复出现的字符时将之前的字符串截掉,剩下的进行递归,这样效率非常低下,而且频繁截取字符串,造成TLE

错误解法:

public int LengthOfLongestSubstring(string s) {
        string[] ss = new string[s.Length];
            int index = 0;
            int len = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (!ss.Contains(s.Substring(i, 1)))
                {
                    ss[i] = s.Substring(i, 1);
                    len++;
                }
                else
                {
                    for (int j = 0; j < ss.Length; j++)
                    {
                        if (ss[j] == s.Substring(i, 1))
                        {
                            index = j;
                            break;
                        }
                    }
                    s = s.Substring(index + 1, s.Length - index - 1);
                    int temp = LengthOfLongestSubstring(s);
                    if (temp > len)
                    {
                        len = temp;
                    }
                    break;
                }
            }
            return len;
    }

错误点①s.Substring(i, 1)可以等价于s[i],返回的是char类型

  ②递归截取剩下的进行下一次是否有重复字符的判断,造成效率低下

优秀解法:

public int LengthOfLongestSubstring(string s) {
        if (s.Length == 0)
            {
                return 0;
            }
            Hashtable osh = new Hashtable();
            int longest = 0;
            int substringStartPosition = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (!osh.ContainsKey(s[i]))
                {
                    osh.Add(s[i], i);
                }
                else if ((int)osh[s[i]] < substringStartPosition)
                {
                    osh[s[i]] = i;
                }
                else
                {
                    longest = System.Math.Max(longest, i - substringStartPosition);
                    substringStartPosition = (int)osh[s[i]] + 1;
                    osh[s[i]] = i;
                }
            }
            return System.Math.Max(s.Length - substringStartPosition, longest);
    }

思路:将每个字符和他的位置作为一个key value存储(不一定要用哈希表,只要能判断键值对即可),每当出现重复的字符时,将位置(value)更新;

记录最长的没有重复字符的长度和开始的位置,每次新的判断开始位置为有重复字符出现时的下一个字符位。

这样的好处就是不用频繁截取字符串,只需要记录相应的位置和长度。

时间: 2024-08-13 09:01:29

Longest Substring Without Repeating Characters (c#)的相关文章

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

[Leetcode] Longest Substring Without Repeating Characters (C++)

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