美文网首页
leetcode-双指针

leetcode-双指针

作者: 噗嗤噗哩噗通 | 来源:发表于2022-01-16 15:22 被阅读0次

    209 :
    给定一个含有 n 个正整数的数组和一个正整数 target 。

    找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。

    class Solution(object):
        def minSubArrayLen(self, target, nums):
            """
            :type target: int
            :type nums: List[int]
            :rtype: int
            """
            if not nums:
                return 0
    
            start=0
            end=0
            ans = len(nums) + 1
            s=0
            while end<len(nums):
                s+=nums[end]
                while s >= target:
                    ans=min(ans , end-start+1 )
                    s-=nums[start]
                    start=start+1
                end=end+1
            
            return 0 if ans == len(nums) + 1 else ans
    
    
        
    
    

    3
    给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

    class Solution(object):
        def lengthOfLongestSubstring(self, s):
            """
            :type s: str
            :rtype: int
            """
            ans,rk=0,0
            s1=set()
            n=len(s)
            if s=="":
                return 0
            
            if s==" ":
                return 1
    
            for i in range(n) :
                if i!= 0 :
                    s1.remove(s[i-1])
    
                while rk < len(s) and s[rk] not in s1:
                    s1.add(s[rk] ) 
                    rk = rk + 1
                ans = max ( ans,len(s1) ) 
                
            return max ( ans,len(s1) ) 
                
    
    
    

    相关文章

      网友评论

          本文标题:leetcode-双指针

          本文链接:https://www.haomeiwen.com/subject/krpfhrtx.html