LeetCode刷题笔录Partition List

用Python模拟随机游走:从一维到三维,直观理解马尔可夫链的常返性 本文通过Python模拟一维到三维的随机游走,直观展示马尔可夫链的常返性特性。使用NumPy和Matplotlib构建模拟器,揭示不同维度下随机游走的返回概率差异,帮助读者理解为什么一维和二维游走具有常返性,而三维游走可能永远迷失。文章包含完整的代码示例和可视化技巧,适合概率论和Python编程爱好者学习实践。 阅读详情

Given 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.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

和之前某个链表题差不多。建一个dummy head作为所有小于x的node的表头。扫描链表,把小于x的加到新头后面并和原链表脱钩。最后把新链表和剩下的元素链表连起来就行了。中间有很多地方容易出错,要小心。

public class Solution {
	public ListNode partition(ListNode head, int x) {
		if (head == null || head.next == null)
			return head;
		ListNode newHead = new ListNode(-1);
		ListNode p = newHead;
		ListNode p1 = head;
		// append all nodes in the front of the list to the new head if nodes
		// are less than x
		while (p1 != null && p1.val < x) {
			p.next = p1;
			p = p.next;
			p1 = p1.next;
			p.next = null;
		}
		// already found the first node that is greater than or equal to x
		ListNode part2Head = p1;
		if (part2Head != null && part2Head.next != null) {
			ListNode p2 = p1.next;
			while (p2 != null) {
				if (p2.val < x) {
					// append p2 to the lower list
					p.next = p2;
					p = p.next;
					p2 = p2.next;
					p1.next = p2;
					p.next = null;
					continue;
				} else {
					// move p1 and p2 forward
					p1 = p2;
					p2 = p2.next;
				}
			}
		}
		p.next = part2Head;
		return newHead.next;
	}
}


更新:

上面的解法有很多地方容易出错,其实有个更方便一点的办法,就是有两个dummy head,一个放比x小的元素一个放大于等于x的node,最后把两个list连起来就行了。这样写出错的概率会小很多。

public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode d1 = new ListNode(-1);
        ListNode d2 = new ListNode(-1);
        
        ListNode pre1 = d1;
        ListNode pre2 = d2;
        ListNode p = head;
        
        while(p != null){
            if(p.val < x){
                pre1.next = p;
                p = p.next;
                pre1 = pre1.next;
                pre1.next = null;
            }
            else{
                pre2.next = p;
                p = p.next;
                pre2 = pre2.next;
                pre2.next = null;
            }
        }
        pre1.next = d2.next;
        d2.next = null;
        return d1.next;
    }
}




深度强化学习 | 基于DQN实现Flappy Bird游戏与分析 在Flappy Bird中,玩家需要通过控制小鸟安全穿过随机长度的水管来得分。本文基于深度Q网络DQN来实现Flappy Bird游戏的自主探索与学习 阅读详情

相关推荐

Zynq7000双核裸机开发实战:如何安全实现共享内存与乒乓RAM设计

本文深入探讨了Zynq7000双核裸机开发中的共享内存与乒乓RAM设计,重点解析了核间通信的Cache一致性处理机制和硬件级实现方案。通过实战代码示例和性能优化技巧,帮助开发者解决双核运行中的访问冲突问题,提升嵌入式系统开发效率。

weixin_29191669的博客 375

LeetCode 刷题笔录 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link

likecool21的专栏 1806

从充电管理芯片里‘偷师’:拆解一个PMOS双向开关电路,搞定你的固态继电器需求

本文深入解析了PMOS双向开关电路在固态继电器应用中的设计要点,重点探讨了防电流倒灌和高侧双向开关的实现技巧。通过拆解充电管理芯片中的PMOS背靠背结构,工程师可以构建高效可靠的固态开关方案,适用于智能硬件和电池管理系统。文章还提供了关键参数选择、电路优化建议及实测数据对比,助力解决电源防反接等工程难题。

weixin_33698043的博客 480

LeetCode刷题笔录 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 这题不能像merge

likecool21的专栏 840

零基础实战:用 Docker 和 vLLM 本地部署 bge-reranker-v2-m3 重排序模型

本文介绍了如何利用Docker容器和vLLM推理框架在本地部署BAAI开发的bge-reranker-v2-m3文本排序模型。

举世誉之而不加劝,举世非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。 5971

零基础DSP实战TMS320F28035:第九节,ADC比较器&EPWM

ADC比较器框图如下所示:-电压-----------------------------输出-电压A>电压B------------------ 1-电压A

sinat_30943509的博客 2534

LeetCode刷题笔录Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. Y

likecool21的专栏 765

光电二极管探测器电流信号处理与指令输出系统

本文提出了一种基于Python的光电二极管电流信号处理系统设计方案。该系统能够实时采集、处理光电二极管输出的电流信号,并根据预设阈值输出控制指令。系统采用模块化架构,包含数据采集、信号处理、阈值检测、指令生成等核心模块。其中数据采集模块提供了模拟器实现和硬件接口框架,支持噪声模拟和脉冲信号生成。系统设计考虑了实时性、可靠性和可扩展性,适用于光强测量、光电检测等多种应用场景。

huanghm88的专栏 1180

LeetCode刷题笔录Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->

likecool21的专栏 764

13.0 针对跳转不存在Activity崩溃处理,改成弹Toast提示,去掉崩溃弹窗,避免app崩溃退出

在开发过程中,在app跳转到某个没注册的Acitivity时,往往会出现崩溃的场景,这样也不好,接下来就从framework中来拦截这个崩溃,改成toast提示,

安卓兼职framework和app工程师的博客 48

apt-get update时出现GPG error:... bionic InRelease:The following signatures were invalid: EXPKEYS解决方案

本文主要介绍了apt-get update时出现W: GPG error: … bionic InRelease: The following signatures were invalid: EXPKEYSIG B7B3B788A8D3785C MySQL Release Engineering E: The repository ‘… bionic InRelease’ is not signed.解决方案,希望能对使用Ubuntu系统的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 11万+

LeetCode刷题笔录 Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to 

likecool21的专栏 755

基于 STM32 的校园电动车充电桩智能计费与过载保护系统设计与实现

本系统基于 STM32F103 主控,通过 HLW8032 电能计量芯片实现精准计费,配合继电器实现过载保护,RC522 模块完成刷卡身份识别,整体方案成本低、可靠性高,适合校园电动车集中充电场景的批量部署。软件采用状态机设计,逻辑清晰,可扩展多路充电口、远程监控等功能。

weixin_42300449的博客 56

LeetCode刷题笔录Remove Duplicates From Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 先写了一个用extra space的方法,

likecool21的专栏 683

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode刷题笔录Sort List

Sort a linked list in O(n log n) time using constant space complexity. 挺有意思的题,这里用了merge sort,可以利用到merge sorted list 那题的代码 /** * Definition for singly-linked list. * class ListNode { *

likecool21的专栏 679

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode刷题笔录Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space?

likecool21的专栏 658

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode刷题笔录 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 这题xiang

likecool21的专栏 641

LeetCode刷题笔录Remove Nth Node from End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the

likecool21的专栏 633

LeetCode刷题笔录Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy t

likecool21的专栏 583

求集合的交集/差集

先利用外部排序使两个磁盘文件有序,然后顺序读取两个文件,使用双指针求交集。在进阶 1 中,我们是通过双指针去做的,最坏情况下我们需要完整遍历。时,O(n log m) 可能明显优于 O(n + m)。的,这就使得我们可以通过从 1~1000 遍历来确定交集。,它们已经按非降序排序,请你返回两个数组的。和上一题的区别在于,这里需要找到重复的元素。返回一个数组,数组中的每个元素在。的每个字符串中都出现的共用字符(**注意:**列表中的整数可以按。较小,可以完整加载到内存,而。正整数组成的一个非空数组,按。

jyyyyx的算法之旅 111

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 202

LeetCode 每日一题 2026/11/2-2026/11/8

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步

AlphaTao的博客 175

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 360

leetcode 3736. 最小操作次数使数组元素相等 III 简单

中的所有元素都等于 3。中的所有元素都等于 5。在一步操作中,你可以将任意单个元素。返回使数组中的所有元素都。

2401_88085478的博客 201

LeetCode 1861. 旋转盒子|Go 双指针解法

j >= 0,如果写成j>0,会漏掉第 0 列,最左侧石头处理失败旋转矩阵行列互换:原 m 行 n 列 → 结果 n 行 m 列,make 数组长度不要写反旋转公式m-1-i:是原总行数 m,不是 n!很容易写成n-1-i区分:本题和轮转数组(189)分组循环不一样!189:所有元素固定移动 k 步,需要 gcd 分组1861:障碍物分割成多个独立段,石头向段末尾挤,用双指针更合适。

2503_92676540的博客 175

LeetCode //C - 1313. Decompress Run-Length Encoded List

The problem involves decompressing a run-length encoded list where each pair of adjacent elements in the input array represents a frequency and value. The solution requires expanding these pairs into the corresponding number of values in the output array.

Made in Code 221
上一篇: LeetCode刷题笔录Combination Sum
下一篇: LeetCode刷题笔录Unique Binary Search Trees II
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值