广告
返回顶部
首页 > 资讯 > 后端开发 > Python >leetcode 3. Longest
  • 838
分享到

leetcode 3. Longest

leetcodeLongest 2023-01-31 07:01:47 838人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

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

题目:

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.

大意是找出最长无重复子串


算法思路:

  这道题应该可以用动态规划做,我用的是double point的方法

  一个指针start指向子串头部,另一个指针end指向子串尾部

  每次递增end,判断end所指字符是否在当前子串中出现过

    • 如果出现过,则将start指针向后移动一位

    • 否则,将end指针向后移动,并更新最长子串长度                           

  最后返回子串最大长度即可


  该算法可以在start指针移动时进行优化,但是我优化后发现结果也没啥变化,就贴原来的代码吧


代码:

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        sLen = len(s)
        if sLen == 0 or sLen == 1:
            return sLen

        letters = list(s)

        hashSet = {}
        hashSet[letters[0]] = 1

        start = 0
        end = 1

        maxLen = 1

        while end != sLen:
            letter = letters[end]

            if hashSet.has_key(letter) and hashSet[letter] > 0:
                hashSet[letters[start]] -= 1
                start += 1
                continue

            hashSet[letter] = 1
            end += 1
            if end - start > maxLen:
                maxLen = end - start

        return maxLen


--结束END--

本文标题: leetcode 3. Longest

本文链接: https://www.lsjlt.com/news/192135.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • leetcode 3. Longest
    题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest subst...
    99+
    2023-01-31
    leetcode Longest
  • Longest Substring Without Repeating Characters
    Given a string, find the length of the longest substring without repeating characters.Examples:Given "abc...
    99+
    2023-06-03
  • 3。leetcode在2N的数组中找出
    1.题目:In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Ret...
    99+
    2023-01-31
    组中 leetcode
  • C++实现leetcode(3.最长无重复字符的子串)
    [LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子串 Given a string, fin...
    99+
    2022-11-12
  • 2008-3-3
    发现eva老是出现问题,版本过时。。现在用了webim就是舒服多了[url]http://www-c2.meebo.com.cn/index.html[/url]注册一下,可以绑定msn和qq,不需要再安装其他软件啦。哈哈ubuntu下安装...
    99+
    2023-01-31
  • 马哥3-3
    命令别名:        alias在shell中定义的别名只在当前shell生命周期有效,别名的有效范围为当前shell进程        unalias  撤销 命令替换        $,  反引号文件名通配:*:任意长度的任意字符?...
    99+
    2023-01-31
    马哥
  • 3.redis集群部署3主3从
    redis集群部署 一:安装redis (使用redis3.0.6版本),同《1.redis安装》1.下载源码$ tar xzf redis-3.0.6.tar.gz$ cd redis-3.0.6$ make   2、编译完成后,在Src...
    99+
    2023-01-31
    集群 redis
  • leetCode
    two sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You m...
    99+
    2023-01-31
    leetCode
  • 2017.12.27 3周3次课
    三周第三次课(12月27日)3.7 su命令3.8 sudo命令3.9 限制root远程登录3.7 su命令su命令就是切换用户的工具,通过su可以在用户之间切换,如果超级权限用户root向普通用户切换不需要密码,而普通用户切换到其它任何用...
    99+
    2023-01-31
  • 3-3 SQL Server 2005数
    3-3 SQL Server 2005数据库优化 u      了解数据库引擎优化顾问基本内容 u      掌握数据库引擎优化顾问的使用 u      掌握通过命令行的方式进行索引的优化——DTA     一个数据库系统的性能依赖于组成...
    99+
    2023-01-31
    SQL Server
  • ×××3
       ...
    99+
    2023-01-31
  • python学习整理--3/3
    今天又重新学起了python这门语言,带着新的目的和又涨一岁的自己,其实早在去年的暑期曾学过一段时间,但是最后无疾而终,这次我真心希望可以掌握一门实用的语言来充实自己,之前的学的不论是c还是java,自我感觉除了做题以外一点都用不上,但感觉...
    99+
    2023-01-31
    python
  • 树莓派3(Raspberry Pi 3)
    ·树莓派3(Raspberry Pi 3)安装Win10 IOT1、格式化SD卡(用SDFormatter工具) 2、下载noobs lite即可(https://www.raspberrypi.org/downloads/noobs/),...
    99+
    2023-01-31
    树莓派 Raspberry Pi
  • ⒉设置 Bash 选项[3-3]
    <!-- @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimS...
    99+
    2023-01-31
    选项 Bash
  • 2007-3-3第一天CCNA课
    主要知识点总结: 1.CISCO类产品常用标识:路由器,二层交换机,三层交换机,局域网,广域网,internet,.Frame-Relay各种画法表示: 2.网络设计模型: 接入层:用户接口; 分发层(会话层):公司出口 核心层:ISP  ...
    99+
    2023-01-31
    CCNA
  • Python -- 操作字符串[3/3]
     1,splitlines() yuan@ThinkPad-SL510:~$ ipython -nobanner  In [1]: multiline_string = """This    ...: is    ...: a multi...
    99+
    2023-01-31
    字符串 操作 Python
  • c-3
    第三章 基本类型: (其值不可以再分解为其它类型。也就是说,基本数据类型是自我说明的)     ××× 字符形 实型(浮点型)   枚举类型                单精度 双精度构造类型: (是根据已定义的一个或多个数据...
    99+
    2023-01-31
  • RH033(3)
    搜索文件            逻辑搜索----建立一张表,可能数据不是最新(搜索方式比较单一,这里不能用文件长度和文件所有者来搜索,必须先建立索引)        物理搜索----很慢,但只要在,肯定能找到  数据平均...
    99+
    2023-01-31
  • QOS-3
      QOS-3     1、拥塞管理(congestion management tool) 拥塞管理工具有: frist in ,frist out (FIFo)   queuing Priority queuing(PQ) Custom...
    99+
    2023-01-31
    QOS
  • 802.1x------3
    六.基本的认证过程: 认证的步骤如下: 用户开机后,通过802.1x客户端软件发起请求,查询网络上能处理EAPOL(EAP Over LAN)数据包的设备 如果某台验证设备能处理EAPOL数据包,就会向客户端发送响应包并要求用户提供合法的...
    99+
    2023-01-31
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作