leetcode

链表

1 .相交链表

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        n1 = headA
        n2 = headB
        while n1!= n2:
            if n1 == None:
                n1 = headB
            else:
                n1 = n1.next
            if n2 == None:
                n2 = headA
            else:
                n2 = n2.next
        return n2

2、反转链表

出错点:curr.next = prev写成curr= prev,指针指向,需加.next。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        curr = head
        next = head
        prev = None
        while curr:
            next = curr.next
            curr.next = prev
            prev = curr
            curr = next
        return prev

3 21. 合并两个有序链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if l1 == None:
            return l2
        elif l2 == None:
            return l1
        elif l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2

4 83. 删除排序链表中的重复元素

出错点:比较值的时候忘记加val了,p1和p1.next进行比较,所以p1.next不能为空

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None:
            return None
        p1 = head
        while p1.next:
            if p1.val == p1.next.val:
                p1.next = p1.next.next
            else:
                p1 = p1.next
        return head

5 19.删除链表的倒数第 N 个结点

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummy = ListNode(0)
        dummy.next = head
        p1 = head
        p2 = dummy
        for i in range(n):
            p1 = p1.next
        
        while p1:
            p1 = p1.next
            p2 = p2.next
        p2.next = p2.next.next
        return dummy.next

6 24. 两两交换链表中的节点

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def swapPairs(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy = ListNode(0)
        dummy.next = head
        pre = dummy
        while pre.next and pre.next.next:
            l1 = pre.next
            l2 = pre.next.next
            next = l2.next
            l1.next = next
            l2.next = l1
            pre.next = l2

            pre = l1
        return dummy.next

排序

1 215. 数组中的第K个最大元素

暴力排序

class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        # num = list(set(nums))
        nums.sort()
        n = len(nums)
        return nums[n-k]

堆排序

class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        minheap = []
        heapify(minheap)
        for num in nums:
            heappush(minheap,num)
            if len(minheap)>k:
                heappop(minheap)
        return minheap[0]

3 栈和队列

1 232. 用栈实现队列

class MyQueue:

    def __init__(self):
        self.s1 = []
        self.s2 = []

    def push(self, x: int) -> None:
        return self.s1.append(x)

    def pop(self) -> int:
        if len(self.s2)==0:
            while self.s1:
                self.s2.append(self.s1.pop())
        return self.s2.pop()

    def peek(self) -> int:
        if len(self.s2)==0:
            while self.s1:
                self.s2.append(self.s1.pop())
        return self.s2[-1]


    def empty(self) -> bool:
        if len(self.s1) == 0 and len(self.s2) == 0:
            return True
        else:
            return False

2 20.有效的括号

class Solution:
    def isValid(self, s: str) -> bool:
        if len(s)%2 == 1:
            return False
        stack = []
        pair = {'(':')','[':']','{':'}'}
        for pa in s:
            if pa in pair:
                stack.append(pa)
                continue
            if len(stack) == 0 or pair[stack.pop()] != pa:
                return False
        
        return len(stack) == 0

3 155. 最小栈

class MinStack:
    def __init__(self):
        self.Data = []
        self.Min = []
    def push(self, val: int) -> None:
        self.Data.append(val)
        if self.Min:
            if self.Min[-1]>val:
                self.Min.append(val)
            else:
                self.Min.append(self.Min[-1])
        else:
            self.Min.append(val)

    def pop(self) -> None:
        self.Min.pop()
        return self.Data.pop()

    def top(self) -> int:
        if self.Data == []:
            return None
        return self.Data[-1]


    def getMin(self) -> int:
        if self.Min == []:
            return None
        return self.Min[-1]

哈希表

1 1、 两数之和

哈希

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = []
        dic = {}
        n = len(nums)
        for i in range(n):
            dic[nums[i]] = i
        for j in range(n):
            num = target - nums[j]
            if num in dic and dic[num] != j:
                result.append(dic[num])
                result.append(j)
                return result

暴力

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = []
        n = len(nums)
        for i in range(0,n):
            for j in range(i+1,n):
                sum = nums[i] +nums[j]
                if sum == target:
                    result.append(i)
                    result.append(j)
                    return result

2 217. 存在重复元素

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        return len(nums) != len(set(nums))

3 290. 单词规律

class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        t = s.split(' ')
        return list(map(pattern.find, pattern)) == list(map(t.index, t))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值