首页 > 其他分享 >Study Plan For Algorithms - Part27

Study Plan For Algorithms - Part27

时间:2024-09-09 11:25:05浏览次数:10  
标签:end max Study len char start Algorithms seen Part27

1. 最长不含重复字符的子字符串
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
方法一:

def lengthOfLongestSubstring(s):
    n = len(s)
    max_len = 0
    start = 0
    char_index = {}

    for end in range(n):
        if s[end] in char_index and char_index[s[end]] >= start:
            start = char_index[s[end]] + 1
        char_index[s[end]] = end
        max_len = max(max_len, end - start + 1)

    return max_len

方法二:

def lengthOfLongestSubstring(s):
    max_len = 0
    start = 0
    seen = {}

    for i, char in enumerate(s):
        if char in seen and seen[char] >= start:
            start = seen[char] + 1
        seen[char] = i
        max_len = max(max_len, i - start + 1)

    return max_len

标签:end,max,Study,len,char,start,Algorithms,seen,Part27
From: https://blog.csdn.net/qq_24058289/article/details/142037062

相关文章

  • Applications of UDTL to Intelligent Fault Diagnosis: A Survey and Comparative St
    文章目录摘要一、引言二、背景和定义A.UDTL定义B.基于UDTL的IFD分类C.基于UDTL的IFD动机D.主干结构三、LABEL-CONSISTENTUDTLA.基于网络的UDTLB.基于实例化的UDTLC.基于映射的UDTLD.基于对抗性的IFD四.LABEL-INCONSISTENTUDTLA.PartialUDTLB.OpenSetUDTLC.Uni......
  • Study Plan For Algorithms - Part24
    1.全排列II题目链接:https://leetcode.cn/problems/permutations-ii/给定一个可包含重复数字的序列nums,按任意顺序返回所有不重复的全排列。classSolution:defpermuteUnique(self,nums:List[int])->List[List[int]]:defbacktrack(nums,path,res,us......
  • Study Plan For Algorithms - Part25
    1.栈的压入、弹出序列输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。方法一:defvalidateStackSequences(pushed,popped):stack=[]len_pushed=len(pushed)stack_index=0i......
  • Study Plan For Algorithms - Part23
    1.跳跃游戏II题目链接:https://leetcode.cn/problems/jump-game-ii/给定一个长度为n的0索引整数数组nums。初始位置为nums[0]。每个元素nums[i]表示从索引i向前跳转的最大长度:0<=j<=nums[i]i+j<n返回到达nums[n-1]的最小跳跃次数。classSolutio......
  • Study Plan For Algorithms - Part22
    1.字符串相乘题目链接:https://leetcode.cn/problems/multiply-strings/给定两个以字符串形式表示的非负整数num1和num2,返回num1和num2的乘积,它们的乘积也表示为字符串形式。classSolution:defmultiply(self,num1:str,num2:str)->str:ifnum1==......
  • Study Plan For Algorithms - Part21
    1.缺失的第一个正数题目链接:https://leetcode.cn/problems/first-missing-positive/给定一个未排序的整数数组nums,请找出其中没有出现的最小的正整数。classSolution:deffirstMissingPositive(self,nums:List[int])->int:n=len(nums)forii......
  • Study Plan For Algorithms - Part20
    1.组合总和题目链接:https://leetcode.cn/problems/combination-sum/给定一个无重复元素的整数数组candidates和一个目标整数target,找出candidates中可以使数字和为目标数target的所有不同组合,并以列表形式返回。classSolution:defcombinationSum(self,ca......
  • Study Plan For Algorithms - Part20
    1.树的子结构输入两棵二叉树A和B,判断B是不是A的子结构。方法一:classTreeNode:def__init__(self,val=0,left=None,right=None):self.val=valself.left=leftself.right=rightdefisSubStructure(A,B):ifnotAornot......
  • Study Plan For Algorithms - Part18
    1.搜索插入位置题目链接:https://leetcode.cn/problems/search-insert-position/给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。classSolution:defsearchInsert(self,nums:List[int],target:......
  • COMP20003 Algorithms and Data Structures Spellcheck Lookup
    Assignment2:SpellcheckLookupGeneralYoumustreadfullyandcarefullytheassignmentspecificationandinstructions.Course:COMP20003AlgorithmsandDataStructures@Semester2,2024DeadlineSubmission:Friday6thSeptember2024@11:59pm(endo......