学习日记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
        
相关推荐
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码5 天前
嵌入式学习路线
学习
毛小茛5 天前
计算机系统概论——校验码
学习
babe小鑫5 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms5 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下5 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。5 天前
2026.2.25监控学习
学习
im_AMBER5 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J5 天前
从“Hello World“ 开始 C++
c语言·c++·学习