学习日记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
        
相关推荐
深海潜水员1 小时前
OpenGL 学习笔记 第一章:绘制一个窗口
c++·笔记·学习·图形渲染·opengl
大、男人3 小时前
DeepAgent学习
人工智能·学习
测试人社区—66793 小时前
提升测试覆盖率的有效手段剖析
人工智能·学习·flutter·ui·自动化·测试覆盖率
崇山峻岭之间3 小时前
C++ Prime Plus 学习笔记026
c++·笔记·学习
疋瓞5 小时前
C++_win_QT6学习《3》_结合qt项目开发学习git仓库相关知识
c++·qt·学习
Clarence Liu6 小时前
redis学习 (1) 基础入门
数据库·redis·学习
qq_571099356 小时前
学习周报二十五
学习
崇山峻岭之间6 小时前
C++ Prime Plus 学习笔记027
c++·笔记·学习
Xudde.6 小时前
BabyPass靶机渗透
笔记·学习·安全·web安全