LeetCode 725. Split Linked List in Parts(分隔链表)
题意:将原链表分隔成k个链表,要求所有分隔的链表长度差异至多为1,且前面的链表长度必须大于等于后面的链表长度。
分析:
(1)首先计算链表总长len
(2)根据len得到分隔的链表长度要么为size,要么为size+1,由于前面的链表长度必须大于等于后面的链表长度,因此,前mod个分隔的链表长度为size+1,其他分隔的链表长度为size
(3) vector<ListNode*> ans(k)----k个ListNode*,每个元素都初始化为NULL:对于原链表为NULL,或是len<k的情况很友好
(4)注意:每个分隔的链表末尾指向NULL
(5)要被这句if(mod-- > 0)坑死了,如果写成if(mod)的话,mod为负的情况也会进if
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
ListNode *tmp = root;
int len = 0;
while(tmp){
tmp = tmp -> next;
++len;
}
int splitlen = len / k;
int mod = len % k;
vector<ListNode*> ans(k);
for(int i = 0; i < k && root; ++i){
int tmplen = splitlen;
if(mod-- > 0) ++tmplen;
ans[i] = root;
for(int j = 0; j < tmplen - 1; ++j){
root = root -> next;
}
tmp = root -> next;
root -> next = NULL;
root = tmp;
}
return ans;
}
};
LeetCode 725. Split Linked List in Parts(分隔链表)的更多相关文章
- 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
https://leetcode.com/problems/split-linked-list-in-parts/ Given a (singly) linked list with head nod ...
- [leetcode]725. Split Linked List in Parts链表分块
思路很简单 按时链表的题做起来很容易犯小错误,思维要缜密 还要多练习啊 做之前最好画算法框图 public ListNode[] splitListToParts(ListNode root, in ...
- 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 ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.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 ...
- Python解Leetcode: 725. Split Linked List in Parts
题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...
- [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 ...
随机推荐
- 线上BUG定位神器(阿尔萨斯)-Arthas2019-0801
1.下载这个jar 2.运行这个jar 3.选取你需要定位的问题应用进程 然后各种trace -j xx.xxx.xx.className methodName top -n 3 这个后面要补充去看, ...
- 《爬虫学习》(一)(HTTP协议)
Http请求: 1.在浏览器中发送一个http请求的过程: 2.url详解: URL是Uniform Resource Locator的简写,统一资源定位符. 一个URL由以下几部分组成 scheme ...
- LeetCode 编辑距离(DP)
题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...
- PTA的Python练习题(十六)
第4章-15 换硬币 挺难的,这里学到一个range的用法: 也就是说range函数能实现顺序和倒序,取决于step是正是负 count = 0 x = int(input()) a = x // 5 ...
- 微信小程序前端坑
链接:https://www.cnblogs.com/showMagic/p/7677551.html
- 获取度量数据:创建服务账户获取访问token
kubectl create clusterrolebinding kubelet-api-test --clusterrole=system:kubelet-api-admin --servicea ...
- Educational Codeforces Round 77 (Rated for Div. 2)D(二分+贪心)
这题二分下界是0,所以二分写法和以往略有不同,注意考虑所有区间,并且不要死循环... #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> ...
- Move-to-front(MTF) and Run-lenght encoding(RLE) algorithms
mtf算法(关于该算法:https://www2.cs.duke.edu/csed/algoprobs/beta/bw1.html): #include <stdio.h> #includ ...
- Vue-使用webpack+vue-cli搭建项目
一.准备 安装NodeJs + 安装Webpack + 配置环境变量 技巧使用: 1. npm 淘宝路径配置:npm config set registry=https://registry.npm. ...
- CCF认证 2019-12-3
分析 后面的数据,坐标分布太离散,不能用一个二位数组来模拟垃圾分布.因此,考虑用一个数组记录每个垃圾点的位置. 先根据x坐标.再根据y坐标进行排序. 再遍历数组中的每一处垃圾点,判断其是否能建回收站( ...