1.题目描述

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.

2.解法分析

要找出最长的字符不重复的字串,由不重复二字很容易让人想到hash表,所以我的思路很自然就到unordered_set上面了,对于字符串的题目,很明显需要遍历字符串,遍历过程中设置一个变量随时记录当前得到字符不重复的最长字符的长度,然后设置一个变量记录以当前字符为结束字符的最长字符不重复的子串的长度,这样一定遇到重复的话可以立马根据这个变量回搜。代码如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(s.length()<=1)return s.length();
        unordered_set<char> myHash;
        
        int LongestLen=0;
        int curLen=0;
        for(int i=0;i<s.length();++i)
        {
            if(myHash.count(s[i])==1)
            {
                for(int j=i-curLen;j<i;++j)
                {
                    if(s[j]==s[i])
                    {
                        curLen = i-j;break;
                    }
                    else 
                    {
                        myHash.erase(s[j]);
                    }
                }
            }
            else
            {
                curLen++;
                if(LongestLen<curLen)LongestLen=curLen;
                myHash.insert(s[i]);
            }
        }
        
        return LongestLen;
    }
};

代码一次AC,很爽!

我的算法在最坏情况下的时间复杂度是不好描述,肯定是大于O(N)的,但是我觉得肯定是小于O(NlgN)的,回搜的代价不会太大,但是我总是在想是不是有其他的精妙的解法呢,结果在网上还真找了个,在discuss里面有个算法,乍一看没看懂:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<int> pos(256, -1);
        int begin = 0, ret = 0;
        for (int i = 0; i < s.size(); ++i) {
            begin = max(begin, pos[s[i]] + 1);
            pos[s[i]] = i;
            ret = max(ret, i - begin + 1);
        }
        return ret;
    }
};

看了几分钟之后拍案叫绝,这个实际上也是一个hash表(即pos),它hash的高明之处是不用回搜,之所以不用回搜,是因为他用pos存放了每个字符最近一次出现时候的位置。

可以看出,大数据集的速度提高了一倍,这也可以看出我的算法回搜的平均长度其实不是很大,当然也是因为我用一个变量记录了可能的回搜范围,不过依然没有这个精妙。

转载于:https://www.cnblogs.com/obama/p/3267063.html

leetcode--Longest Substring Without Repeating Characters相关推荐

  1. [LeetCode] Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. LeetCode——Longest Substring Without Repeating Characters

    原问题 Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  3. LeetCode Longest Substring Without Repeating Characters

    题意 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  4. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  6. [LeetCode]3.Longest Substring Without Repeating Characters

    [题目] Given a string, find the length of the longest substring without repeating characters. For exam ...

  7. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  8. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  9. leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)

    Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...

  10. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

最新文章

  1. SQL Server自带备份整个数据库脚本工具
  2. 深度理解do{} while(0)语句的作用
  3. 产品经理利器之axure rp
  4. 【干货】周鸿祎谈雷军:能不能All In是一个核武器
  5. php多选批量修改数据库数据类型有哪些_php面试题之memcache和redis的区别
  6. 资深美术分享:游戏开发如何确定画风?
  7. 云中的机器学习:FPGA 上的深度神经网络
  8. form表单中的input有哪些类型
  9. C语言 | 递增运算符
  10. java 接口和虚构_深入理解Java的接口和抽象类
  11. Ubuntu系统下载地址(Ubuntu、ISO、Ubuntu下载)
  12. air写文件 SecurityError: fileWriteResource 时报错的解决方法
  13. SQL0286N 找不到页大小至少为 8192、许可使用授权标识 db2inst 的缺省表空间。...
  14. C语言哈希表uthash的使用方法详解(附下载链接)
  15. linux ssh工具下载,ssh下载_ssh工具下载-太平洋下载中心
  16. 据说是可减少50%的超越卓越经验
  17. 见证历史时刻!关于朗道-西格尔零点猜想
  18. 手机cpu天梯图2020
  19. linux轻量级进程,linux轻量级进程LWP
  20. 基于C++控制台(Windows平台)的一个吃豆人小游戏

热门文章

  1. rango php,韩天峰(Rango)的博客
  2. java 读取内存地址结构体_Java并发系列之volatile
  3. python fortran混合编程_python fortran c 混合编程
  4. es的分片数量和扩展性分析
  5. VB 两种方法建立多级目录
  6. 联系实际谈谈管理者的沟通协调能力建设
  7. 2018年ML/AI重大进展有哪些?LeCun推荐了这篇回答
  8. 用机器学习拯救“智障”聊天机器人,谷歌开放分析平台Chatbase
  9. 2019年上海地区最新JAVAEE开发面试与笔试题
  10. Spring Security(5):自定义决策