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 ...
随机推荐
- 【Linux设备驱动程序】Chapter 1 - 概述
设备分类 字符设备.块设备.网络接口 字符设备 能够像字节流(类似文件)一样被访问的设备. 字符设备驱动程序通常至少要实现 open.close.read 和 write 系统调用. 举例:字符终端( ...
- 《转》oracle—flashback
FLASHBACK介绍 在介绍flashback之前先介绍下undo_retention相关参数 undo_retention:表示undo数据的过期时间.系统默认这个时间设置为900即15分钟.但要 ...
- ajax 参数data问题 data中的 参数名 参数值为string 提交到后台后,会自动转换参数名相同的 类型 和 js字符串拼接
latlng"14.6005238,100.43635419999998"Cusid"accb5c1b-6aef-4f3b-a4eb-d60ea1ca5f54" ...
- FreeBSD Top States
转自:http://blog.csdn.net/fcoolx/article/details/4412196 select Process is blocked in the select(2) sy ...
- 自实现部分string类的功能
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class MyString { publi ...
- delete与delete [] 真正差别
我们通常从教科书上看到这种说明: delete 释放new分配的单个对象指针指向的内存 delete[] 释放new分配的对象数组指针指向的内存 那么,依照教科书的理解,我们看下以下的代码: int ...
- pandas所占内存释放
df = pd.read_csv('....') 要调用循环处理多个文件时,内存占用情况严重,如果互相之间不需要调用,可以直接del df 释放内存
- Jetty - Handler源码分析
1. 描述 基于Jetty-9.4.8.v20171121. Handler是Jetty服务处理器,用户Server处理HTTP请求. Handler可以做如下处理: (1)完全生成HTTP响应: ( ...
- 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释
转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体 ...
- Ubuntu下键盘输入错乱问题,输入双引号输出的是@符号,输入#号输出的是未知语言的字符
装完搜狗后,键盘开始出现混乱,切换到英文输入法,输入双引号输出的是@符号,输入#号输出的是未知语言的字符. 网上有的说在 system - keyboard - Input Source 下看看是否是 ...