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

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

相关推荐
淮北4942 小时前
STL学习(十一、常用的算数算法和集合算法)
c++·vscode·学习·算法
糖葫芦君2 小时前
玻尔兹曼分布与玻尔兹曼探索
人工智能·算法·机器学习
花火|7 小时前
算法训练营day37 动态规划⑤ 完全背包 518. 零钱兑换 II、 377. 组合总和 Ⅳ、70. 爬楼梯 (进阶)
算法·动态规划
Neil今天也要学习7 小时前
永磁同步电机无速度算法--脉振方波注入法
算法
小学生的信奥之路7 小时前
力扣1116题:用C++实现多线程交替输出零、偶数、奇数
c++·leetcode·多线程
绿炮火8 小时前
【MATLAB】(二)基础知识
开发语言·算法·matlab
88号技师8 小时前
2025年6月最新SCI-灰熊脂肪增长优化算法Grizzly Bear Fat Increase-附Matlab免费代码
开发语言·人工智能·算法·matlab·优化算法
玄月初二丶9 小时前
28. 找出字符串中第一个匹配项的下标
c语言·开发语言·数据结构·算法
qq_427506089 小时前
JavaScript和小程序写水印的方法示例
前端·算法·微信小程序
小猪扒饭9 小时前
C基础 12_day
c语言·笔记·学习·算法