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

题目的意思是求字符串中最长的无重复子串长度,如上面的
 abcabcbb    最长无重复子串为abc  长度为3

直接暴力法:代码如下

class Solution {
public:
    int lengthOfLongestSubstring(string s) {

       int i,j,temp,result=1;

        for(i=0;i<s.length();i++)
        {
            temp=1;
            for(j=0;j<s.length()-i;j++)
            {
                if(check(s,i,j))
                    temp++;
                else
                    continue;
            }

            result=max(result,temp);
        }

        return result;

    }
    bool check(string s,int i,int j)
    {
        char ch=s[j];

        for(int t=i;t<j;t++)
        {
            if(ch==s[t])
            {
                return false;
            }
        }
        return true;
}
};

暴力法虽然简单,但时间过不了,下面用一个复杂度为n的算法

思路:

用一个256位数组asc[](字符共有256个,包括128个扩展字符),字符串中每一个字符的ASCII值对应该字符在数组中的位置

如字符a
 在   asc[a]中的下标为97

初始化数组asc,令每一个元素为-1。定义start,start表示每次无重复字符串的其实点,初始值为0.从i=0开始遍历字符串,如果数组asc中没有出现该字符(asc[ch]==-1),那么令asc[ch]==i,记录下来该字符在字符串中的位置,接着遍历,如果遇到asc[ch]!=-1表示该字符已经出现了,需要将start到j的asc数组中的元素抹掉。然后重置start,令start=max(asc[i]+1,start);然后将

最后结果为   result=max(result,i-start);

代码如下:

<span style="font-size:18px;">class Solution {
public:
    int lengthOfLongestSubstring(string s) {

        int result=0;
        int a[128];
        int i,j,t,start=0;

        for(i=0;i<128;i++)
            a[i]=-1;

        for(i=0;i<s.length();i++)
        {

            if(a[s[i]]!=-1)
            {
               if(result<i-start)
                    result=i-start;

                for(j=start;j<a[s[i]];j++)
                    a[j]=-1;
                start=max(start,a[s[i]]+1);
              //  result=max(result,i-start);
            }

            a[s[i]]=i;

        }
        result=max(result,i-start);
        return result;

    }
};</span>
时间: 2024-12-24 00:36:41

Longest Substring Without Repeating Characters 字符串中最长的无重复子串长度的相关文章

Given a string, find the length of the 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——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

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

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

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

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

LeetCode之 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 o