▶ 将一个单链表拆分为长度尽量接近的 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的更多相关文章

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

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

  3. #Leetcode# 725. Split Linked List in Parts

    https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...

  4. 【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 ...

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

  6. 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. Python解Leetcode: 725. Split Linked List in Parts

    题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...

  8. LeetCode 725. Split Linked List in Parts(分隔链表)

    题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度. 分析: (1)首先计算链表总长len (2)根据len得到分隔的链表长度要么为size, ...

  9. [leetcode]725. Split Linked List in Parts链表分块

    思路很简单  按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...

随机推荐

  1. 获取文本中所有的<img>标签的位置,获取所有img标签的src

    public static int[] GetImagePos(string str) { str = str.Replace("$", " "); str = ...

  2. Tornado源码分析 --- Etag实现

    Etag(URL的Entity Tag): 对于具体Etag是什么,请求流程,实现原理,这里不进行介绍,可以参考下面链接: http://www.oschina.net/question/234345 ...

  3. 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存

    首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...

  4. Leetcode 16

    //一次AC 有点爽的class Solution { public: int threeSumClosest(vector<int>& nums, int target) { ; ...

  5. Hibernate入门2.简单的项目开发实例

    Hibernate入门2.简单的项目开发实例 这一节通过一个简单的项目学习Hibernate项目的配置 代码下载 : 链接: http://pan.baidu.com/s/1zlgjl 密码: p34 ...

  6. qt忙等与非忙等

    非忙等: void delay(int msec) { QTime end = QTime::currentTime().addMSecs(msec); while( QTime::currentTi ...

  7. sgu 146. The Runner 取模技巧 难度:1

    146. The Runner time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard inputou ...

  8. SGU 139. Help Needed! 逆序数,奇偶性,分析 难度:0

    139. Help Needed! time limit per test: 0.25 sec. memory limit per test: 4096 KB Little Johnny likes ...

  9. Winform开发常用控件之DataGridView的简单数据绑定——自动绑定

    DataGridView控件可谓是Winform开发的重点控件,对于数据的呈现和操作非常方便,DataGridView可谓是既简单又复杂.简单在于其已经集成了很多方法,复杂在于可以使用其实现复杂的数据 ...

  10. Centos7 服务 service 设置命令 systemctl 用法 (替代service 和 chkconfig)

    在Centos 中 systemctl  是设置系统服务的命令,即 service  ,   它融合之前service和chkconfig的功能于一体. 可以使用它永久性或只在当前会话中启用/禁用服务 ...