725. Split Linked List in Parts把链表分成长度不超过1的若干部分
[抄题]:
Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".
The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.
The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.
Return a List of ListNode's representing the linked list parts that are formed.
Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]
Example 1:
Input:
root = [1, 2, 3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The input and each element of the output are ListNodes, not arrays.
For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null.
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but it's string representation as a ListNode is [].
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么平均分配,以为是dp,结果除一下就可以了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
k组中,每一组开个头,然后n+r位用两个节点往后贴
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 往后复制链表需要两个节点,一个node,一个prev。
- 数linkedlist的长度需要一个辅助节点node。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 往后复制链表需要两个节点,一个node,一个prev。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
//root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
//Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]] class Solution {
public ListNode[] splitListToParts(ListNode root, int k) {
//initialization
ListNode[] result = new ListNode[k]; //corner case
if (root == null || k <= 0) return result; //for loop for i and r: add each node for n times
int len = 0;
for (ListNode node = root; node != null; node = node.next) {
len++;
} int n = len / k; int r = len % k;
ListNode node = root; ListNode prev = null;
for (int i = 0; i < k && node != null; i ++, r--) {
//for loop for j: add a r if necessary
result[i] = node;
for (int j = 0; j < n + (r > 0 ? 1 : 0); j++) {
prev = node;
node = node.next;
}
prev.next = null;
}
//return
return result;
}
}
725. Split Linked List in Parts把链表分成长度不超过1的若干部分的更多相关文章
- LeetCode 725. Split Linked List in Parts (分裂链表)
Given a (singly) linked list with head node root, write a function to split the linked list into k c ...
- LC 725. Split Linked List in Parts
Given a (singly) linked list with head node root, write a function to split the linked list into k c ...
- 【Leetcode】725. Split Linked List in Parts
Given a (singly) linked list with head node root, write a function to split the linked list into k c ...
- 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 725. Split Linked List in Parts
▶ 将一个单链表拆分为长度尽量接近的 k 段 ● 自己的代码,12 ms ■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = m ...
- Python解Leetcode: 725. Split Linked List in Parts
题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...
- LeetCode 725. Split Linked List in Parts(分隔链表)
题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...
- [leetcode]725. Split Linked List in Parts链表分块
思路很简单 按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...
- [LeetCode] Split Linked List in Parts 拆分链表成部分
Given a (singly) linked list with head node root, write a function to split the linked list into k c ...
随机推荐
- verilog 代码分析与仿真
verilog 代码分析与仿真 注意:使用vivado 自带的仿真工具, reg和wire等信号需要赋予初始值 边沿检测 module signal_test( input wire cmos_pcl ...
- EasyMock 模拟对象测试
一.EasyMock 使用动态代理实现模拟对象创建,一般可以满足以下测试需求 1.要测试的模块依赖于其它自己控制不了的模块,如第三方服务,其它组员在开发的服务等,它们都没办法配合你来测试: 2.涉及到 ...
- sql server 索引碎片相关问题
1.查看表的索引碎片情况 --改成当前库 use DB_Name --创建变量 指定要查看的表 declare @table_id int set @table_id=object_id('Table ...
- HTML/CSS基础知识(四)
WEB标准和W3C的理解与认识 Web标准是一系列标准的集合. 网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior). 对应的标准也分三方面:结 ...
- Numpy学习笔记(二)
(1)NumPy - 切片和索引 l ndarray对象中的元素遵循基于零的索引. 有三种可用的索引方法类型: 字段访问,基本切片和高级索引. l 基本切片 Python 中基本切片概念到 n 维 ...
- linux下自定义dubbo的shell脚本
- C#设计模式(3)——工厂方法模式(Factory Method)
在简单工厂模式中通过工厂Factory获取不同的对象,但是有一个明显的缺点——简单工厂模式系统难以扩展! 一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过于复杂, 可以通过工厂 ...
- Sphinx 与全文索引
全文索引创建过程 第一步:将源文档传给分词组件(Tokenizer) 分词组件做了以下事情: 将文档分成一个一个的单词 去除标点符号 去除停词:英文(the / a / this / that ... ...
- 卷积神经网络(CNN)张量(图像)的尺寸和参数计算(深度学习)
分享一些公式计算张量(图像)的尺寸,以及卷积神经网络(CNN)中层参数的计算. 以AlexNet网络为例,以下是该网络的参数结构图. AlexNet网络的层结构如下: 1.Input: 图 ...
- Week__8
Monday_ 今晚补了扔鸡蛋问题的动态规划问题,补了这道题,感觉视野又开阔了些. 写了一道思维题cf 1066A 数字逻辑后半节听得打脑壳,现在很晚了,明天再看叭. Tuesday_ 今晚补了 ad ...