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

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

相关推荐
2401_831920743 分钟前
C++代码国际化支持
开发语言·c++·算法
m0_6727033110 分钟前
上机练习第51天
数据结构·c++·算法
ArturiaZ17 分钟前
【day60】
算法·深度优先·图论
2401_8512729929 分钟前
自定义内存检测工具
开发语言·c++·算法
☆5661 小时前
C++中的命令模式
开发语言·c++·算法
仰泳的熊猫1 小时前
题目2577:蓝桥杯2020年第十一届省赛真题-走方格
数据结构·c++·算法·蓝桥杯
CoovallyAIHub1 小时前
Pipecat:构建实时语音 AI Agent 的开源编排框架,500ms 级端到端延迟
深度学习·算法·计算机视觉
灰色小旋风1 小时前
力扣13 罗马数字转整数
数据结构·c++·算法·leetcode
2301_810160951 小时前
C++与物联网开发
开发语言·c++·算法
cm6543202 小时前
基于C++的操作系统开发
开发语言·c++·算法