美文网首页
算法与数据结构

算法与数据结构

作者: wenyilab | 来源:发表于2021-02-17 22:58 被阅读0次

基础数据结构

  • 数组、链表、跳表的原理和实现
类型 链接
数组 https://blog.csdn.net/qq_30257149/article/details/99588189
链表 http
跳表 http

代码:


  • 三者的空间复杂度、时间复杂度
  • 工程运用
  • 跳表:升维思想+空间换时间

实战

盛水最多的容器

def maxArea(height):
    l,r = 0,len(higth)-1
    ans = 0
    while l < r:
        area = min(height[l],height[r]) * (r-l)
        ans = max(area,ans)
        if height[l] < height[r]:
            l += 1
        else:
            r -= 1
    return ans

爬楼梯

def climbStairs(n):
    a = b = 1
    for _ in range(n):
        a,b = b,(a+b)
    return a

移动零

def moveZeros(nums):
    j = 0
    for i in range(len(nums)):
        if nums[i] != 0:
            nums[j] = nums[i]
            if i != j:
                nums[i] = 0
            j += 1

链表:
reverse-linked-list

swap-nodes-in-pairs

linked-list-cycle

linked-list-cycle-ii

revese-nodes-in-k-group

Homework
remove-duplicates-from-sorted-array

rotate-array

merge-sorted-array

two-sum

move-zeroes

plus-one

34. 在排序数组中查找元素的第一个和最后一个位置

class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        leftindex = self.binary(nums,target,True)
        rightindex = self.binary(nums,target,False)-1
        if leftindex <= rightindex and rightindex < len(nums) and nums[leftindex] == target and nums[rightindex] == target:
            return [leftindex,rightindex]
        return [-1,-1]

    def binary(self,nums,target,lower):
        left = 0
        right = len(nums)-1
        ans = len(nums)

        while left <= right:
            mid =(left + right) // 2
            if (nums[mid] >  target) or (lower and nums[mid] >= target):
                right = mid - 1
                ans = mid 
            else:
                left = mid + 1
        return ans

120. 三角形最小路径和

class Solution:
    def minimumTotal(self, triangle: List[List[int]]) -> int:
        n = len(triangle)
        f = [[0] * n for _ in range(n)]
        f[0][0] = triangle[0][0]

        for i in range(1,n):
            f[i][0] = triangle[i][0] + f[i-1][0]

            for j in range(1,i):
                f[i][j] = min(f[i-1][j],f[i-1][j-1]) + triangle[i][j]

            f[i][i] = triangle[i][i] + f[i-1][i-1]

        return min(f[n-1])

113. 路径总和 II

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
        res = []
        path = []

        def dfs(root,targetSum):
            if not root:
                return
            
            path.append(root.val)
            targetSum -= root.val 
            if not root.left and not root.right and targetSum == 0:
                res.append(path[:])
            dfs(root.left,targetSum)
            dfs(root.right,targetSum)

            path.pop()

        dfs(root,targetSum)
        return res

560. 和为K的子数组

class Solution:
    def subarraySum(self, nums: List[int], k: int) -> int:
        count,pre = 0,0
        mp = {}
        mp[0] = 1
        for i in range(len(nums)):
            pre += nums[i]
            if pre-k in mp:
                count += mp.get(pre - k)
            mp[pre] = mp.get(pre,0) + 1
        return count

剑指 Offer 18. 删除链表的节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
        if head.val == val:return head.next

        tmp = head
        while tmp.next :
            if tmp.next.val == val:
                tmp.next = tmp.next.next
                return head 
            tmp = tmp.next
        return head 

剑指 Offer 22. 链表中倒数第k个节点

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

class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
        slow,fast = head,head
        t = 0
        while fast:
            if t >= k: slow = slow.next
            fast = fast.next
            t += 1
        return slow

剑指 Offer 24. 反转链表

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

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        preNode = None
        curNode = head

        while curNode:
            nextNode = curNode.next
            curNode.next = preNode
            preNode = curNode
            curNode = nextNode
        return preNode

剑指 Offer 25. 合并两个排序的链表

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

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        cur = dum = ListNode(0)
        while l1 and l2:
            if l1.val < l2.val:
                cur.next,l1 = l1,l1.next
            else:
                cur.next,l2 = l2,l2.next
            cur = cur.next
        cur.next = l1 if l1 else l2
        return dum.next

剑指 Offer 26. 树的子结构

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
        def recur(A,B):
            if not B: return True
            if not A or A.val != B.val:return False
            return recur(A.left,B.left) and recur(A.right,B.right)
        return bool(A and B)  and (recur(A,B) or self.isSubStructure(A.left,B) or self.isSubStructure(A.right,B))

剑指 Offer 27. 二叉树的镜像

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def mirrorTree(self, root: TreeNode) -> TreeNode:
        if not root: return
        stack = [root]
        while stack:
            node = stack.pop()
            if node.left:stack.append(node.left)
            if node.right:stack.append(node.right)
            node.left,node.right = node.right,node.left
        return root

剑指 Offer 28. 对称的二叉树

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        def recur(L,R):
            if not L and not R:return True
            if not L or not R or L.val != R.val:return False
            return recur(L.left,R.right) and recur(L.right,R.left)
        return recur(root.left,root.right) if root else True

剑指 Offer 29. 顺时针打印矩阵

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if not matrix : return []
        l,r,t,b,res = 0,len(matrix[0])-1,0,len(matrix)-1,[]

        while True:
            for i in range(l,r+1):res.append(matrix[t][i])
            t += 1
            if t > b:break
            for i in range(t,b+1):res.append(matrix[i][r])
            r -= 1
            if l > r:break
            for i in range(r,l-1,-1):res.append(matrix[b][i])
            b -= 1
            if t > b:break
            for i in range(b,t-1,-1):res.append(matrix[i][l])
            l += 1
            if l > r:break
        return res 

剑指 Offer 33. 二叉搜索树的后序遍历序列

class Solution:
    def verifyPostorder(self, postorder: List[int]) -> bool:
        def recur(i,j):
            if i >= j:return True
            p = i
            while postorder[p] < postorder[j]: p += 1
            m = p 
            while postorder[p] > postorder[j]: p += 1
            return p == j and recur(i,m-1) and recur(m,j-1)

        return recur(0,len(postorder)-1)

剑指 Offer 38. 字符串的排列

class Solution:
    def permutation(self, s: str) -> List[str]:
        c = list(s)
        res = []

        def dfs(x):
            if x == len(c)-1:
                res.append(''.join(c))
            setc = set()
            for i in range(x,len(c)):
                if c[i] in setc: continue
                setc.add(c[i])

                c[x],c[i] = c[i],c[x]
                dfs(x+1)
                c[i],c[x] = c[x],c[i]
        dfs(0)
        return res 

剑指 Offer 39. 数组中出现次数超过一半的数字

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        votes = 0
        for num in nums:
            if votes == 0:x = num 
            if x == num : votes += 1
            else : votes -= 1
        return x 

剑指 Offer 40. 最小的k个数

import heapq
class Solution:
    def getLeastNumbers(self, arr: List[int], k: int) -> List[int]:
        if k == 0: return []
        hq = [-x for x in arr[:k]]

        heapq.heapify(hq)
        for i in range(k,len(arr)):
            if -hq[0] > arr[i]:
                heapq.heappop(hq)
                heapq.heappush(hq,-arr[i])
        ans = [-x for x in hq]
        return ans

剑指 Offer 41. 数据流中的中位数

class MedianFinder:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.A = []
        self.B = []

    def addNum(self, num: int) -> None:
        if len(self.A) != len(self.B):
            heapq.heappush(self.A,num)
            heapq.heappush(self.B,-heapq.heappop(self.A))
        else:
            heapq.heappush(self.B,-num)
            heapq.heappush(self.A,-heapq.heappop(self.B))


    def findMedian(self) -> float:
        return self.A[0] if len(self.A) != len(self.B) else (self.A[0] - self.B[0]) / 2


# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()

剑指 Offer 42. 连续子数组的最大和

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        dp = [0] * len(nums)
        dp[0] = nums[0]
        for i in range(1,len(nums)):
            if dp[i-1] > 0:
                dp[i] = dp[i-1] + nums[i]
            else:
                dp[i] = nums[i]
        return max(dp)

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        for i in range(1,len(nums)):
            nums[i] += max(nums[i-1],0)
        return max(nums)

剑指 Offer 45. 把数组排成最小的数

class Solution:
    def minNumber(self, nums: List[int]) -> str:
        def quick_sort(l,r):
            if l >= r :return 
            i,j = l,r
            while i < j:
                while strs[j] + strs[l] >= strs[l] + strs[j] and i < j : j -= 1
                while strs[i] + strs[l] <= strs[l] + strs[i] and i < j : i += 1
                strs[i],strs[j] = strs[j],strs[i]
            strs[i],strs[l] = strs[l],strs[i]
            quick_sort(l,i-1)
            quick_sort(i+1,r)
        
        strs = [str(num) for num in nums]
        quick_sort(0,len(nums)-1)
        return "".join(strs)

剑指 Offer 47. 礼物的最大价值

class Solution:
    def maxValue(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])

        for j in range(1,n):
            grid[0][j] += grid[0][j-1]
        for i in range(1,m):
            grid[i][0] += grid[i-1][0]

        for i in range(1,m):
            for j in range(1,n):
                grid[i][j] += max(grid[i-1][j],grid[i][j-1])

        return grid[-1][-1]

相关文章

  • 数据结构与算法 - 查找

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构数据结构...

  • 思维导图之数据结构+算法

    数据结构+算法 = 程序 数据结构比较 参考文章 数据结构与算法数据结构与算法(java)

  • 数据结构与算法 - 树形结构

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构 目录 ...

  • 最新完整数据结构与算法

    最新完整数据结构与算法 P11_课程介绍 P22_数据结构与算法概述_数据结构 P33_数据结构与算法概述_算法 ...

  • 数据结构与算法

    数据结构与算法之美 数据结构与算法之美1--如何学数据结构与算法之美2--复杂度分析(上)数据结构与算法之美3--...

  • 算法与数据结构(1),List

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 习惯了,深夜更新博客...

  • 数据结构与算法-目录

    数据结构与算法-目录 C语言篇 数据结构和算法-C语言篇1-绪论数据结构和算法-C语言篇2-初识算法数据结构与算法...

  • 算法与数据结构(3),并发结构

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 本来已经合上电脑了,...

  • 算法与数据结构(2),Map

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 睡了不到六个小时,被...

  • 数据结构与算法

    参考链接:算法 数据结构与算法 iOS数据结构 和 算法 上 算法 1、数据结构: 集合结构: 线性结构: 树形结...

网友评论

      本文标题:算法与数据结构

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