学习日记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
        
相关推荐
Slow菜鸟2 小时前
AI学习篇(五) | awesome-design-md 使用说明
人工智能·学习
狐狐生风3 小时前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
狐狐生风3 小时前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
努力努力再努力FFF5 小时前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
sakiko_6 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Alice-YUE7 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
北山有鸟8 小时前
修改源码法和插件法
嵌入式硬件·学习
richxu202510018 小时前
嵌入式学习之路->stm32篇->(14)通用定时器(上)
stm32·单片机·嵌入式硬件·学习
qeen879 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
lizhihai_999 小时前
股市学习心得-六张分时保命图
大数据·人工智能·学习