Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法
归并排序能够有两种思路----top-down 和 bottom-up
top-down:
递归实现,将数组分成两半。分别处理。再合并。
伪代码例如以下:
split ( A[], l, r)
{
if ( r - l < 2) return;
m = (r + l) / 2;
split ( A, l, m); //split A[l…m-1]
split ( A, m, r); //split A[m…r-1]
merge ( A, l, m, e); //merge A[l…m-1] and A[m…e-1]
}
bottom-up:
循环实现。将数组看做n个长度为1的组数组。
用width控制每次merge的子数组长度。width每次翻倍
伪代码例如以下:
sort ( A[], n)
{
for (width = 1; width < n; width *= 2)
{
for (i = 0; i < n; i += 2 * width)
{
merge(A, i, min(i + width, n), min(i + 2 * width, n));
}
}
}
Sort list中使用链表。不能在O(1)的时间内訪问随意节点,同一时候注意要处理尾部节点的next,置为NULL
和上面的伪代码类似,首先实现merge函数:
ListNode * merge(ListNode * h1, int s1, ListNode * h2, int s2)
{
if (h2 == NULL) return h1;
ListNode * h;
if (h1->val < h2->val)
h = advance(h1, s1);
else
h = advance(h2, s2);
ListNode * cur = h;
while (s1 && s2)
{
if (h1->val < h2->val)
cur->next = advance(h1, s1);
else
cur->next = advance(h2, s2);
cur = cur->next;
}
if (s1)
{
cur->next = h1;
while(s1) advance(h1, s1);
}
if (s2)
{
cur->next = h2;
while(s2) advance(h2, s2);
}
return h;
} ListNode * advance(ListNode * (& n), int & size)
{
ListNode * temp = n;
if (size == 1) n->next = NULL;
n = n->next;
size--;
return temp;
}
同一时候实现工具函数,訪问任何位置节点
ListNode * getNode(ListNode * head, int len)
{
while (len -- && head) head = head->next;
return head;
}
循环版本号主函数例如以下:
ListNode *sortList(ListNode *head) {
ListNode * cur = head;
int size = 0;
while (cur)
{
size ++;
cur = cur->next;
} ListNode * pre;
for (int w = 1; w <= size; w *= 2)
{
cur = head;
for (int i = 0; i < size; i+= w*2)
{
ListNode * h1 = cur, * h2 = getNode(cur, w), * next = getNode(cur, 2 * w);
cur = merge(h1, min(w, size - i), h2, min(w, size - i - w));
if (i == 0)
head = cur;
else
pre->next = cur;
pre = getNode(cur, min(2 * w, size - i) - 1);
cur = next;
}
}
return head;
}
递归版本号主函数例如以下:
ListNode *sortList(ListNode *head) {
ListNode * cur = head;
int size = 0;
while (cur)
{
size ++;
cur = cur->next;
}
return sort(head, size - size / 2, getNode(head, size - size / 2), size / 2);
} ListNode * sort(ListNode * h1, int s1, ListNode * h2, int s2)
{
if (s1 == 0) return h2;
if (s2 == 0) return h1;
h1 = sort(h1, s1 - s1 / 2, getNode(h1, s1 - s1 / 2), s1 / 2);
h2 = sort(h2, s2 - s2 / 2, getNode(h2, s2 - s2 / 2), s2 / 2);
return merge(h1, s1, h2, s2);
}
Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法的更多相关文章
- Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法
Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. fs.listFiles方法,返回Loc ...
- LeetCode算法题-Intersection of Two Arrays(Java实现-四种解法)
这是悦乐书的第207次更新,第219篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第75题(顺位题号是349).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...
- leetcode-91-解码方法(动态规划和递归两种解法)
题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LeetCode] Validate Binary Search Tree (两种解法)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Java实现 LeetCode 787 K 站中转内最便宜的航班(两种DP)
787. K 站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是 ...
- LeetCode算法题-Missing Number(Java实现-四种解法)
这是悦乐书的第200次更新,第209篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第65题(顺位题号是268).给定一个包含n个不同数字的数组,取自0,1,2,...,n ...
- LeetCode算法题-Ugly Number(Java实现-四种解法)
这是悦乐书的第199次更新,第208篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第64题(顺位题号是263).编写一个程序来检查给定的数字是否是一个丑陋的数字.丑陋的数 ...
- C#版 - LeetCode 148. Sort List 解题报告(归并排序小结)
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total ...
随机推荐
- Thread.sleep(0)的作用
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7 12:00:00.000,如果我调用 ...
- mysql处理大数据合并的另一种方法
在做项目的时候我碰到了一个这样的场景,有两张表,数据都在300W左右,现在要求在生产环境中合并这两张表为一张表,本来想用sql语句insert into select来实现,后来发现这样操作速度很慢, ...
- Linux修改时间的方法
http://www.blogjava.net/itvincent/archive/2007/08/03/134242.html修改linux的时间可以使用date指令 在命令行输入: date 显示 ...
- Bootstrap学习笔记 Well
Well是一种会引起内容凹陷或插图效果的容器div.为了创建Well,只需要简单地把内容放在带有class well的div中即可.下面的实例演示了默认的Well: html: <div> ...
- mysql主从复制原理及实现
一.主从复制原理 利用MySQL提供的Replication,其实就是Slave从Master获取Binary log文件,然后再本地镜像的执行日志中记录的操作.由于主从复制的过程是异步的,因此Sla ...
- checkbox 全选操作
<html> <head></head> <body> <div id="places"> <input type ...
- const和readonly关键字
不知道大家对const和readonly这两个关键字的区别有什么了解,原来自己之前还真不清楚它们到底是怎么回事,那么如果你也不是很清楚的话,可以一起来探讨一下.在了解这两个关键字的时候我们先来了解一下 ...
- STL容器分析--queue
queue,顾名思义,是指队列.满足先进先出的原则.
- Java 装饰模式(4.4)
装饰模式(decorator pattern). 依照Num模型.讨论职业/IProfession类层次. IProfession定义了方法say(String),事实上现类教师/ Teacher.医 ...
- HTTPSConnectionPool(host='xxxxx', port=443): Max retries exceeded with url:xxxxxxxx (Caused by NewConnectionError('<urllib3.connect,Max retries exceeded with ,(Caused by NewConnectionError
HTTPSConnectionPool(host='f6ws-sha8re-o88k.s3.ama66zaws.com', port=443): Max retries exceeded with u ...