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 ...
随机推荐
- 使用outflux 导入influxdb 的数据到timescaledb
influxdb 以及timescaledb 都是不错的时序数据库,timescaledb 团队提供了直接从influxdb 导入 环境准备 docker-compose 文件 version: &q ...
- lsof一些使用
查看某进程和哪些文件相关 [root@linux-node2 ~]# netstat -lntp Active Internet connections (only servers) Proto Re ...
- Vue 中使用 viewerjs
安装 viewerjs npm install viewerjs --save 创建一个 Viewer.vue 组件 <template> <div id="index&q ...
- Java方法的静态绑定与动态绑定讲解(向上转型的运行机制详解)
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6554103.html 一:绑定 把一个方法与其所在的类/对象 关联起来叫做方法的绑定.绑定分为静态绑定(前期绑 ...
- Padavan老毛子固件:17CE插件集成
Padavan老毛子固件:17CE插件集成 1.老毛子路由设置:系统管理-服务-启动SSH服务器 以下链接下载 "winscp" http://down.orsoon.co ...
- 华硕飞马3S,日常使用续航测试
最近爱机荣耀6的电池1天2充,无奈换台新机,华为系列没大电池且价格贵,小米红米系列品控呵呵,其他品牌无小屏幕大容量电池: 然后换了台华硕飞马3S:5.2英寸 5000ma电池,日常工作娱乐使用1天半多 ...
- 【mysql】mysql触发器使用示例
mysql触发器 时间点:before/after 触发事件: update/delete/insert 时间点+触发事件:构成一个完整的触发器的触发时机: 一个触发时机最多只能由1个Trigger: ...
- SpringCloud和Springboot
SpringBoot+SpringCloud+SpringMVC+SpringData 我们把这种架构也称之为spring全家桶 什么是SpringCloudSpring Cloud是一系列框架的有序 ...
- 使用Java监控工具出现 Can't attach to the process
问题重现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ➜ jinfo -flags 3032 Attaching ...
- windows copy 和xcopy
#将文件夹下文件拷贝到指定目录 将d盘下1文件夹内所有内容拷贝到测试目录下copy d:\1\ Z:\chenkai\测试目录\ /yxcopy D:\soft\svn工具 Z:\chenkai\测试 ...