学习日记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
        
相关推荐
衡石科技13 分钟前
Data_Agent记忆机制与经验复用衡石分析智能体会话记忆与长期学习技术解析
人工智能·科技·学习·算法·企业级bi
传奇开心果编程38 分钟前
【Xilem基础语法学与练】第7课:Xilem与Masonry底层构建技术解析
学习·rust·前端框架
想要入门的程序猿1 小时前
回调函数学习
java·网络·学习
码上上班1 小时前
iptables学习; firewalld学习;ufw学习
学习
RisunJan1 小时前
丝连族谱 0452 — 功能拆解与使用手册
学习
老王爱玩车2 小时前
深入理解指针2
c语言·开发语言·学习
那年窗外下的雪.3 小时前
AIDC 学习日志|第 22 天|All-Active/Single-Active 故障演练设计
服务器·网络·学习·算法·哈希算法
龚寿生3 小时前
23-敏捷开发实战:从Scrum到看板的团队协作方法论
android·学习·scrum·敏捷流程
cc_yy_zh4 小时前
学习使用kali抓包
服务器·学习·php