学习日记day42

Day42_1204

专注时间:2h58min

每日任务:计算机网络50分钟(0分钟),搜广推90分钟~上不封顶+手撕目录里的算法(0),二刷hot100算法题2道(完成4道),刑法实务考试复习50分钟(已经看完。不用再看了)

学习内容: 如上,好像都在做算法题,下午放学之后没去图书馆,所以没有专注时长,非常可惜、非常浪费了。

总结与心得: 明天不做算法题。

《54.螺旋矩阵》

python 复制代码
class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        m = len(matrix)
        n = len(matrix[0])
        visited =[[False for _ in range(n)] for _ in range(m)]
        dx = [0,1,0,-1]
        dy = [1,0,-1,0]
        x,y = 0,0
        d=0
        res = []
        for k in range(n*m):
            res.append(matrix[x][y])
            visited[x][y]=True
            newx = x + dx[d]
            newy = y + dy[d]
            if 0<=newx<m and 0<=newy<n and visited[newx][newy]==False:
                x = newx
                y = newy
            else:
                d = (d+1)%4
                x+=dx[d]
                y+=dy[d]
        return res

《48.旋转图像》

python 复制代码
class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix)
        i,j = 0,n-1
        while i<j:
            for k in range(n):
                matrix[i][k],matrix[j][k] = matrix[j][k],matrix[i][k]
            i+=1
            j-=1
        for i in range(n):
            for j in range(i+1):
                matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j]
        
python 复制代码
class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        n = len(matrix)
        helper = [[0 for _ in range(n)] for _ in range(n)]
        for j in range(n-1,-1,-1):
            for i in range(n):
                helper[i][j] = matrix[n-1-j][i]
        for i in range(n):
            for j in range(n):
                matrix[i][j] = helper[i][j]
                

《240.搜索二维矩阵②》

python 复制代码
class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        m = len(matrix)
        n = len(matrix[0])
        for j in range(m):
            if matrix[j][n-1]>=target:
                for i in range(n):
                    if matrix[j][i] == target:
                        return True
        return False
        
python 复制代码
class Solution(object):
    def erfen(self,nums,target):
        l,r = 0,len(nums)-1
        while l<r:
            mid = (l+r)/2
            if nums[mid]>=target:
                r = mid
            else:
                l = mid+1
        return nums[l]==target
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        m = len(matrix)
        n = len(matrix[0])
        for j in range(m):
            if matrix[j][n-1]>=target:
                if matrix[j][n-1]==target:
                    return True
                if self.erfen(matrix[j],target) is True:
                    return True
        return False
python 复制代码
class Solution(object):
    def searchMatrix(self, matrix, target):
        """
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        #z字形搜索
        m = len(matrix)
        n = len(matrix[0])
        x,y = 0,n-1
        while x<m and y>=0:
            if matrix[x][y]==target:
                return True
            elif matrix[x][y]>target:
                y-=1
            else:
                x+=1  
        return False

《160.相交链表》

python 复制代码
# 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
        """
        if not headA or not headB:
            return None

        p,q = headA,headB
        while p!=q:
            p = p.next if p else headB
            q = q.next if q else headA
        return p
python 复制代码
# 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
        """
        if not headA or not headB:
            return None
        occ = set()
        q = headA
        while q:
            occ.add(q)
            q=q.next
        p = headB
        while p:
            if p in occ:
                return p
            p=p.next
        return None
        
相关推荐
龙文浩_4 小时前
Attention Mechanism: From Theory to Code
人工智能·深度学习·神经网络·学习·自然语言处理
爱上好庆祝5 小时前
svg图片
前端·css·学习·html·css3
嵌入式小企鹅6 小时前
蓝牙学习系列(八):BLE L2CAP 协议详解
网络·学习·蓝牙·ble·协议栈·l2cap
jiayong237 小时前
第 8 课:开始引入组合式函数
前端·javascript·学习
格鸰爱童话7 小时前
向AI学习项目技能(五)
java·学习
技术人生黄勇7 小时前
拆解 Hermes Agent:开源 Agent 里唯一的闭环学习系统
学习
凉、介8 小时前
别再把 PCIe 的 inbound/outbound、iATU 和 eDMA 混为一谈
linux·笔记·学习·嵌入式·pcie
speop9 小时前
TASK01 | Reasoning Kindom
学习
2301_822703209 小时前
鸿蒙flutter三方库实战——教育与学习平台:Flutter Markdown
学习·算法·flutter·华为·harmonyos·鸿蒙
码喽7号9 小时前
vue学习四:Axios网络请求
前端·vue.js·学习