leetcode接着写

20. 有效的括号

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        '''while '{}' in s or '()' in s or '[]' in s:
            s = s.replace('{}', '')
            s = s.replace('[]', '')
            s = s.replace('()', '')
        return s == ''
        '''
        stack = []
        for item in s:
            if item == '(':
                stack.append(')')
            elif item == '[':
                stack.append(']')
            elif item == '{':
                stack.append('}')
            elif not stack or stack[-1] != item:
                return False
            else:
                stack.pop()
        
        if not stack:
            return True
        else:
            return False

92. 反转链表 II

class Solution(object):
    def reverseBetween(self, head, left, right):
        """
        :type head: ListNode
        :type left: int
        :type right: int
        :rtype: ListNode
        """
        if not head or not head.next or left==right:
            return head
        dummy = ListNode(0)
        dummy.next = head
        start = dummy
        for i in range(left-1):
            start = start.next       
        end = cur = start.next  
        pre = None
        for i in range(right-left+1):
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp         
        start.next = pre
        end.next = cur
        return dummy.next

46. 全排列

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """

        res = []  #存放符合条件结果的集合
        path = []  #用来存放符合条件的结果
        def backtrack(nums):
            if len(path) == len(nums):
                return res.append(path[:])  #此时说明找到了一组
            for i in range(len(nums)):
                if nums[i] in path:  #path里已经收录的元素,直接跳过
                    continue
                path.append(nums[i])
                backtrack(nums)  #递归
                path.pop()  #回溯
        backtrack(nums)
        return res

64. 最小路径和

class Solution(object):
    def minPathSum(self, grid):       
        #此数组用于记忆化搜索
        dp = [[0 for j in range(len(grid[0]))] for i in range(len(grid))]
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                #在起点的时候
                if i == 0 and j == 0:
                    dp[i][j] = grid[0][0]
                #在左边缘的时候
                elif j == 0 and i != 0:
                    dp[i][j] = dp[i - 1][j]  + grid[i][j]
                #在上边缘的时候
                elif i == 0 and j != 0:
                    dp[i][j] = dp[i][j-1] + grid[i][j]
                # 普遍情况下
                else:
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
                    
        return dp[len(grid)-1][len(grid[0])-1]

984. 不含 AAA 或 BBB 的字符串

class Solution(object):
    def strWithout3a3b(self, a, b):
        """
        :type a: int
        :type b: int
        :rtype: str
        """
        A = a 
        B = b
        if A >= B*2:
            return "aab"*B + "a"*(A-2*B)
        if B >= A*2:
            return "bba"*A + "b"*(B-2*A)
        if A == B:
            return "ab"*A
        if A > B:
            return "aab"*(A-B) + "ab"*(2*B-A)
        if B > A:
            return "bba"*(B-A) + "ba"*(2*A-B)

239. 滑动窗口最大值 (华为一面手撕代码题)

class Solution(object):
    def maxSlidingWindow(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        dq = deque() #队列,先进先出
        ans = []
        for i in range(len(nums)):
            if dq and dq[0] <= i-k: # 清除超出滑动窗口范围的元素
                dq.popleft()
            while dq and nums[dq[-1]] <= nums[i]:  # 清除小于当前值的元素
                    dq.pop()
            dq.append(i)
            ans.append(max(nums[dq[0]], nums[i]))
        return ans[k-1:]

3. 无重复字符的最长子串 (重要但是不会!)

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        last = {}
        start = 0
        res = 0
        for i in range(len(s)):
            if s[i] in last:
                start = max(start, last[s[i]] +1)
            last[s[i]] = i 
            res = max(res, last[s[i]]-start+1)
        return res

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值