725. Split Linked List in Parts
▶ 将一个单链表拆分为长度尽量接近的 k 段
● 自己的代码,12 ms
■ 记链表长度为 count,目标段数为 k,quo = count / k,mod = count % k,part = mod * (quo + 1)
■ 前半截(长半截)共有 mod 组,每组 quo + 1 个元素,共 mod * (quo + 1) 个元素,这是 part 的由来;后半截(长半截)共有 k - mod 组,每组 quo 个元素,共 quo * (k - mod) 个元素
■ 当 i < part 时,第 i 元素处于前半截,组号 s = i / (quo + 1),该组最后一个元素下标为 t = (quo + 1) * (s + 1) - 1,即满足 (t + 1) % (quo + 1) == 0
■ 当 i >= part 时,第 i 元素处于后半截,组号 s = (i - part) / quo + mod = (i - mod) / quo,该组最后一个元素下标为 t = (s + 1) * quo + mod - 1 (前面所有组的元素个数,注意偏移量 mod),即满足 (t + 1 - mod) % quo == 0
class Solution
{
public:
vector<ListNode*> splitListToParts(ListNode* root, int k)
{
vector<ListNode *> table(k, nullptr);
if (root == nullptr)
return table;
int count, i;
ListNode *p, *q;
for (p = root, count = ; p->next != nullptr; p = p->next, count++);// 计算结点数
const int quo = count / k, mod = count % k, mod * (quo + );
for (p = table[] = root, i = ; p != nullptr && p->next != nullptr; i++)
{
if (i < part && !((i + ) % (quo + )))// p 指向了前半截某组的末尾结点
{
q = p->next, p->next = nullptr, p = q;
table[(i + ) / (quo + )] = q; // 注意此时是在table 中挂上 q 指向的结点,相当于第 i + 1 个结点
}
else if (i >= part && !((i + - mod) % quo))// p 指向了后半截某组的末尾结点
{
q = p->next, p->next = nullptr, p = q;
table[(i + - mod) / quo] = q;
}
else
p = p->next;
}
return table;
}
};
● 大佬的代码,11 ms,使用简单的判断 idx < remainder 来确认切分位置
class Solution
{
public:
vector<ListNode*> splitListToParts(ListNode* root, int k)
{
if (k == )
return vector<ListNode*>{ root };
vector<ListNode*> res(k, nullptr);
ListNode *temp;
int len, idx, tmp;
for (len = , temp = root; temp != nullptr; len++, temp = temp->next);
const int per_len = len / k, remainder = len % k; for (idx = ; idx < k; )
{
tmp = per_len + (idx < remainder ? : );
if (tmp == )
{
res[idx++] = nullptr;
continue;
}
for (res[idx++] = root; tmp != ; root = root->next, tmp--);
temp = root->next, root->next = nullptr, root = temp;
} return res;
}
};
● 大佬的方法,11 ms,号称不需要知道链表的长度。每次指针 slow 移动一格,指针 fast 移动 k 格,直到 fast 抵达链表尾部,这时 slow 大约移动了 n / k 格,即为分界点。实际上 fast 在整个过程中移动了 O(n2) 的次数,还不如提前一趟遍历计算链表的长度
class Solution
{
public :
vector<ListNode *> splitListToParts(ListNode *root, int k)
{
vector<ListNode *> res(k, nullptr);
ListNode *fast, *slow;
int i, step;
for (i = ; i < k; i++)
{
if (root == nullptr)
break;
for (slow = root, fast = root, step = k;;)
{
fast = move(fast, step);
if (fast != nullptr)
slow = slow->next;
else
break;
}
res[i] = root;
if (slow->next != nullptr)
{
root = slow->next;
slow->next = nullptr;
}
else
break;
step--;
}
return res;
}
ListNode* move(ListNode *node, int step)
{
for(;step > ;)
{
node = node->next;
step--;
if (node == nullptr)
break;
}
return node;
}
};
725. Split Linked List in Parts的更多相关文章
- 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 ...
- 725. Split Linked List in Parts把链表分成长度不超过1的若干部分
[抄题]: Given a (singly) linked list with head node root, write a function to split the linked list in ...
- #Leetcode# 725. Split Linked List in Parts
https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...
- 【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 (分裂链表)
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 ...
- 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 ...
随机推荐
- BOM之其他浏览器对象的使用
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- js enter键激发事件
document.onkeydown = function (e) { if (!e) e = window.event; if ((e.keyCode | ...
- Linux:root下的文件-anaconda-ks.cfg详解
anaconda-ks.cfg详解 系统安装的时候生成的一个文件,通过这个文件可以修改成自动安装的脚本,用于自动安装同样配置的系统. 自动生成的启动文件anaconda# Kickstart file ...
- cetos7 systemd 详解
CentOS7/RHEL7 systemd详解 目录1. 为什么是systemd(1) 关于Linux服务管理(2) SysV init的优缺点(3) UpStart的改进(4) systemd的 ...
- Python流程控制-while循环-for循环
写重复代码 是可耻的行为 -------------- 完美的分割线 -------------- 摘录自:http://www.runoob.com/python/python-loops.htm ...
- I.MX6 7" navigation bar as black bar
/********************************************************************************* * I.MX6 7" n ...
- It is the courage
It is the reality that a society which becomes lower and becomes weak.Believe it or not,I think it i ...
- Codeforces 990B :Micro-World
B. Micro-World time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 便捷的Jenkins jswidgets
很多时候我们在构建完成之后需要查看构建的状态,类似github 中的build Status 插件安装 搜索插件 使用 目前好像只支持自由项目的构建 代码集成 <!DOCTYPE html> ...
- 转 HTTP/2: The Long-Awaited Sequel
HTTP/2: The Long-Awaited Sequel Thursday, October 9, 2014 2:01 AM 6 Ready to speed things up? Here a ...