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

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

相关推荐
你怎么知道我是队长44 分钟前
C语言---循环结构
c语言·开发语言·算法
艾醒1 小时前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法
纪元A梦3 小时前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
_不会dp不改名_3 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
mark-puls3 小时前
C语言打印爱心
c语言·开发语言·算法
Python技术极客3 小时前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法
吃着火锅x唱着歌4 小时前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun4 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌4 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
我星期八休息4 小时前
深入理解跳表(Skip List):原理、实现与应用
开发语言·数据结构·人工智能·python·算法·list