一天两道力扣(2)

python 复制代码
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def middleNode(self, head):
        slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow
    def reverseList(self, head):
        pre, cur = None, head
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt
        return pre

    def isPalindrome(self, head):
        mid = self.middleNode(head)
        head2 = self.reverseList(mid)
        while head2:
            if head.val != head2.val:
                return False
            head = head.next
            head2 = head2.next
        return True
        

解析:先找到中间位置,然后从中间反转, 再对比

python 复制代码
class Solution(object):
    def dailyTemperatures(self, temperatures):
        length = len(temperatures)
        ans = [0] * length
        stack = []
        for i in range(length):
            temperature = temperatures[i]
            while stack and temperature > temperatures[stack[-1]]:
                pre_index = stack.pop()
                ans[pre_index] = i - pre_index
            stack.append(i)
        return ans

解析:单调栈里面存的是下标,遇到大的就弹出来且记录,不大就继续存着

相关推荐
C30_027 分钟前
10.4作业
算法
lingchen19061 小时前
b = [1 2 3;4 5 6;7 8 9]>> b(2,2)=[ ]??? Subscripted assignme
数据结构·算法
Mr_Xuhhh2 小时前
哈希扩展学习
学习·算法·哈希算法
sxtyjty2 小时前
ABC426G - Range Knapsack Query
c++·算法·分治
dog2502 小时前
时延抖动的物理本质
人工智能·算法·机器学习
Vect__2 小时前
二叉树实战笔记:结构、遍历、接口与 OJ 实战
数据结构·c++·算法
hahaha60162 小时前
高层次综合基础-vivado hls第三章
算法·fpga开发
smallnetter4 小时前
华为OD机试C卷 - 分苹果 - 二进制 - (Java & C++ & JavaScript & Python)
算法·华为od
爱和冰阔落4 小时前
【C++ STL栈和队列下】deque(双端队列) 优先级队列的模拟实现与仿函数的介绍
开发语言·数据结构·c++·算法·广度优先