算法:分区列表86. Partition List

这篇具有很好参考价值的文章主要介绍了算法:分区列表86. Partition List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

86. Partition List

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.

Example 1:
算法:分区列表86. Partition List

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]

Example 2:

Input: head = [2,1], x = 2
Output: [1,2]

Constraints:

  • The number of nodes in the list is in the range [0, 200].
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

双链表解法

Solution
The problem wants us to reform the linked list structure, such that the
elements lesser that a certain value x, come before the elements greater or
equal to x. This essentially means in this reformed list, there would be a
point in the linked list before which all the elements would be smaller than
x and after which all the elements would be greater or equal to x.
Let’s call this point as the JOINT.
算法:分区列表86. Partition List
Reverse engineering the question tells us that if we break the reformed list
at the JOINT, we will get two smaller linked lists, one with lesser elements
and the other with elements greater or equal to x. In the solution, our main aim
is to create these two linked lists and join them.文章来源地址https://www.toymoban.com/news/detail-501175.html

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        before = before_head = ListNode(0)
        after = after_head = ListNode(0)
        while head:
            if head.val < x:
                before.next = head
                before = before.next
            else:
                after.next = head
                after = after.next
            head = head.next
        
        after.next = None
        before.next = after_head.next
        return before_head.next

到了这里,关于算法:分区列表86. Partition List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Hive-Partition】Hive添加分区及修改分区location

    当我们在 Hive 中创建外表时,需要映射 HDFS 路径,数据落入到 HDFS 上时,我们在 Hive 中查询时会发现 HDFS中有数据,Hive 没有数据,那是因为我们在 HDFS 上的数据还没有成功修复至 Hive中,需要进行分区的修复(或者称之为添加分区) 注意 :如果数据量较大,或分区数量较多

    2024年02月13日
    浏览(65)
  • 【LeetCode 算法】Linked List Cycle II 环形链表 II

    给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从

    2024年02月14日
    浏览(32)
  • Leetcode 1325. Delete Leaves With a Given Value (二叉树后序遍历经典题)

    Delete Leaves With a Given Value Solved Medium Topics Companies Hint Given a binary tree root and an integer target, delete all the leaf nodes with value target. Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot)

    2024年02月22日
    浏览(31)
  • 原生 Kafka 消费时无限报 Error deserializing key/value for partition 问题

    背景 使用 kafka-clients.jar 中的 原生 API 消费 Kafka 数据时, consumer.poll 操作遇到了一个异常: 自定义的消费线程遇到这个异常后,一直刷该异常。网上给的解决办法都是使用 spring-kafka 设置异常处理器实现的,但是对于原生 API 怎么办呢? 解决办法 搜到一篇文章 《如何最好地

    2024年02月14日
    浏览(28)
  • 大数据篇Kafka消息队列指定Topic打印Key、Value、Offset和Partition

    说到Apache Kafka消息传递系统时,以下是一些关键概念的解释: Key(键):Kafka消息由Key和Value组成。Key是一个可选的字段,它通常用于消息的路由和分区策略。Key的目的是确保具有相同Key的消息被写入同一个分区。当消费者接收到消息时,可以使用Key来进行消息处理和路由操

    2024年02月16日
    浏览(47)
  • Kafka中的主题(Topic)和分区(Partition)是什么?它们之间有什么关系?

    在Kafka中,主题(Topic)和分区(Partition)都是用于组织和存储消息的概念,它们有密切的关系。 主题(Topic):主题是消息的逻辑分类。可以将主题理解为一个逻辑上的消息容器,类似于一个消息类别或者话题。在Kafka中,生产者(Producer)将消息发布到特定的主题,而消费

    2024年02月15日
    浏览(35)
  • [架构之路-178]-《软考-系统分析师》- 分区操作系统(Partition Operating System)概述

    目录: 本文概述: 1.1 什么是分区操作系统 1.2 分区操作系统出现背景 1. 前后台系统(Foreground/Background System) 2. 实时操作系统(RTOS) 随着 嵌入式系统 日趋 复杂化 以及对 安全性 要求的不断提高,采用 空间隔离 、时间预先分配的 分时分区 操作系统已经成为未来的发展方向。

    2024年02月01日
    浏览(46)
  • esp32-idf eclipse 分区表(partition table / NVS)的读写demo

    前言: 分区表(Partition Table)和 NVS(Non-Volatile Storage)是 ESP-IDF 中用于存储数据的两种不同机制。 分区表(Partition Table): 分区表定义了将 Flash 存储器划分为不同逻辑分区的方式。每个分区都具有特定的大小、起始地址和类型,可以用于存储不同类型的数据,例如应用程序

    2024年01月23日
    浏览(17)
  • 详解kafka中的消息日志文件:Topic消息分类、partition分区、segment分段、offset偏移量索引文件

    Kafka是一种高吞吐量的基于zookeeper协调的以集群的方式运行的分布式发布订阅消息系统,支持分区(partition)、多副本(replica),具有非常好的负载均衡能力和处理性能、容错能力。Kafka采用发布/订阅模型,消息生产者将消息发送到Kafka的消息中心(broker)中,然后消费者从

    2024年02月03日
    浏览(76)
  • 全网最详细地理解Kafka中的Topic和Partition以及关于kafka的消息分发、服务端如何消费指定分区、kafka的分区分配策略(range策略和RoundRobin策略)

    最近在学习kafka相关的知识,特将学习成功记录成文章,以供大家共同学习。 首先要注意的是, Kafka 中的 Topic 和 ActiveMQ 中的 Topic 是不一样的。 在 Kafka 中, Topic 是一个存储消息的逻辑概念,可以认为是一个消息集合。每条消息发送到 Kafka 集群的消息都有一个类别。 物理上

    2024年01月25日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包