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 ...
随机推荐
- JAVA异常处理分析高级进界(下)
既然Throwable是异常处理机制的核心,那么,我们就来分析下它的源码来看看它是如何实现的. 进行分析前,我们可以先想想如果让我们实现一个异常处理机制,我们需要它做什么? 1. 发生异常终止程序执行 ...
- [转]Linux下彻底卸载mysql详解
http://www.jb51.net/article/97516.htm 一.使用以下命令查看当前安装mysql情况,查找以前是否装有mysql 1 rpm -qa|grep -i mysql 可以 ...
- Leetcode 12
//日积月累,水滴石穿class Solution { public: string longestCommonPrefix(vector<string>& strs) { if ...
- lister.ora配置
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = PLSExtProc) (ORACLE_HOME = D:\ ...
- 055——VUE中vue-router之路由参数的随意设置与伪静态链接地址处理:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 002——php字符串中的处理函数(一)
<?php /** * 字符串处理函数: * 一.PHP处理字符串的空格: * strlen 显示字符串长度 * * trim 对字符串左右空格删除: * ltrim 对字符串左侧空格删除 * ...
- bzoj2843&&1180
题解: lct 和上一题差不多 这一题还要判断是否有链接 其实直接并查集判断就可以了 代码: #pragma GCC optimize(2) #include<bits/stdc++.h> ...
- 【LeetCode 1_数组_哈希表】Two Sum
解法一:O(N) vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, i ...
- instancetype 与id
1 .依照cocoa的命名规则,alloc,init这类方法,如果以id为返回类型,会返回类本身的类型,但类方法的返回类型,LLVM(clang)编译器无法判断,也就是说如果 用id作为返 ...
- Property 'submit' of object #<HTMLFormElement> is not a function
<form action="" type="get" id="form"> <input type="butto ...