LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

题目:

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.

解题思路:

  这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3。有这么几个方法:

方法一:

  依赖字符串本身的一些特有函数,进行相应操作来完成。我们可以维护一个子串,来保存最长的无重复的子串,并记录当前子串的长度,如果遇到重复的字符,则去掉子串中重复的字符,一次进行下去,最终就能找到最长无重复子串。如str = ababc, substr = a, ab, ba, ab, abc....类似这样的思路。如下代码:

//方法一:string only
int lengthOfLongestSubstring(string s)
{
    size_t j = 1;
    if (s.size() <= 1)
        return s.size();

    int len = 1, nMaxLen = 0;
    string subStr;
    subStr.push_back(s[0]);
    while (j < s.size()) {
        if (subStr.find(s[j]) == string::npos) {
            subStr.push_back(s[j]);
        }
        else {
            if (len > nMaxLen)
                nMaxLen = len;
            while (subStr.find(s[j]) != string::npos) {
                subStr.erase(0,1);
                len --;
            }
            subStr.push_back(s[j]);
        }
        len ++;
        j ++;
    }
    if (len > nMaxLen)
        nMaxLen = len;
    return nMaxLen;
}

方法二:

  指针法:用一个指针指向字符串的左边界,如果遇到重复的字符,就往后移动,同时用一个有26位的字符数组(因为总共就26个字符)来保存每一个字符最近一次出现的位置,以此来更新指针位置和字符位置之间的距离,就可以算出最长无重复字符的长度,如下代码所示:

 1 //方法二:pointer
 2 int lengthOfLongestSubstring2(string s) {
 3     int maxlen = 0, left = 0;
 4     int sz = s.length();
 5     int prev[26];
 6     memset(prev, -1, sizeof(prev));
 7
 8     for (int i = 0; i < sz; i++) {
 9         if (prev[s[i]-‘a‘] >= left) {
10             left = prev[s[i]-‘a‘] + 1;
11         }
12         prev[s[i]-‘a‘] = i;
13         maxlen = max(maxlen, i - left + 1);
14     }
15     return maxlen;
16 }

方法三:

  hashtable法:该方法和方法二其实是同一个思路,只不过该方法我不用数组来存字符的位置,而是通过hashtable来存,进而提高效率。如下代码:

 1 //方法三:hash table
 2 int lengthOfLongestSubstring3(string s) {
 3     if(s.length()<2)
 4         return s.length();
 5     int max_len=0;
 6     map<char,int> sub; //hash map
 7     for(int i=0,j=0;i<s.length();++i){
 8         if(sub.find(s[i])!=sub.end()){
 9             j=max(j,sub[s[i]]+1);
10         }
11         sub[s[i]]=i;
12         max_len=max(max_len,i-j+1);
13     }
14     return max_len;
15 }
时间: 2024-10-10 04:28:16

LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium的相关文章

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

LeetCode 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

[Swift]LeetCode3. 无重复字符的最长子串 | 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-无重复字符的最长子串

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 字符数组和字符串的区别,C语言字符数组和字符串区别详解 开始的想法是在对字符数组设置两个指针,初始化一个在位置0一个在位置1,ans初始化为1(非空),然后1向后找,0不动,取0到1之间的字符串,判断是否重复(这里单独写一个函数,输入字符串,输出是否重复以及重复字符的后一个的位置),不

Leetcode——3. 无重复字符的最长子串

难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一个字符串,请你找出其中不含有重复字符的?最长子串?的长度. 示例?1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字

Leetcode(3)无重复字符的最长子串

Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果:太差 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ Maxsize=0 res='' if len(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

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