一天两道力扣(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

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

相关推荐
艾醒29 分钟前
huggingface入门:如何使用国内镜像下载huggingface中的模型
算法
艾醒39 分钟前
huggingface入门:Tokenizer 核心参数与实战指南
算法
啊我不会诶1 小时前
【图论】拓扑排序
算法·深度优先·图论
浩浩乎@1 小时前
【openGLES】着色器语言(GLSL)
人工智能·算法·着色器
张同学的IT技术日记2 小时前
【奇妙的数据结构世界】 用经典例题对数组进行全面分析 | C++
算法
queenlll2 小时前
Codeforces Round 1043 (Div. 3)
算法
摸鱼一级选手2 小时前
十大经典 Java 算法解析与应用
java·算法·排序算法
Ldawn_AI4 小时前
4+ 图论高级算法
算法·深度优先·图论
Xの哲學4 小时前
Linux PCI 子系统:工作原理与实现机制深度分析
linux·网络·算法·架构·边缘计算
NuyoahC6 小时前
笔试——Day46
c++·算法·笔试