2021-05-12

一、英语

1、mortifying:令人羞愧的、令人窘迫的
2、detect with a critical eye:用吹毛求疵的眼光去看待
3、attend to:关心、关注

二、算法题目

1、769:数组index的巧妙应用

class Solution:
    def maxChunksToSorted(self, arr: List[int]) -> int:
        #倒序的数字必须在一个块中进行排序,否则连接之后也是无序的
        #这里数字是从1到n-1,这时候数组的index是可以利用的信息
        #记录当前的最大值,如果最最大值大于index,那么说明index右边还有比max小的数,那么要继续扩大块范围
        #如果max==index,那么说明考虑了所有的逆序,最大的为max,进行一次分块
        n = len(arr)
        res = 0
        max_n = 0
        for i in range(n):
            max_n = max(max_n,arr[i])
            if max_n == i:
                res += 1
                #这里后面的分段一定比已经分段的数字大
                #所以max_n这里可以不用初始化为0
                #max_n = 0
        return res

2、232:数据结构:栈和队列

class MyQueue:
    #两个list
    #push:直接加入到list1中
    #pop:将list1中的元素从尾道头赋值给list2,并将list1清空,pop返回的是list2的尾部元素
    #peek:如果list2不为空,那么fron就是list2尾部元素,如果list2为空,那么就是list1的第一个元素
    #empty:list1和list2都为空

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.list1 = []
        self.list2 = []


    def push(self, x: int) -> None:
        """
        Push element x to the back of queue.
        """
        self.list1.append(x)


    def pop(self) -> int:
        """
        Removes the element from in front of queue and returns that element.
        """
        #用完之后再向list2中加入数字,这样可以保证顺序的正确,而不是list1接收一个就加入一个
        if self.list2 == []:
            for i in self.list1[::-1]:
                self.list2.append(i)
            self.list1.clear()
        return self.list2.pop()



    def peek(self) -> int:
        """
        Get the front element.
        """
        if self.list2:
            return self.list2[-1]
        else:
            return self.list1[0]


    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        return self.list1==[] and self.list2==[]



# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

生活得向前看
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值