【leetcode】sort list(python)
链表的归并排序
超时的代码
class Solution:
def merge(self, head1, head2):
if head1 == None:
return head2
if head2 == None:
return head1
# head1 and head2 point to the same link list
if head1 == head2:
return head1 head = None
tail = None
# the small pointer point to smaller of two.
while head1 and head2:
if head1.val <= head2.val:
small = head1
head1 = head1.next
else:
small = head2
head2 = head2.next
# the first node
if tail == None:
tail = small
head = small
else:
tail.next = small
tail = small
# link the remaind nodes
if head1 == None:
head1 = head2 tail.next = head1
return head def sortList(self, head):
if head == None or head.next == None:
return head
# we use a fast pointer which go two steps each time and
# a slow pointer which go one step each time to get the
# middle of the link list
slow = head
fast = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
# slow point to the middle now
head2 = slow.next
# we cut of the linked list at middle
slow.next = None left = self.sortList(head)
right = self.sortList(head2)
return self.merge(left, right)
主要是在merge的时候。要推断第一个结点
AC代码
class Solution:
def merge(self, head1, head2):
if head1 == None:
return head2
if head2 == None:
return head1
# head1 and head2 point to the same link list
if head1 == head2:
return head1 head = ListNode(-1)
tail = head
# the small pointer point to smaller of two.
while head1 and head2:
if head1.val <= head2.val:
small = head1
head1 = head1.next
else:
small = head2
head2 = head2.next tail.next = small
tail = small
# link the remaind nodes
if head1 == None:
head1 = head2 tail.next = head1
return head.next def sortList(self, head):
if head == None or head.next == None:
return head
# we use a fast pointer which go two steps each time and
# a slow pointer which go one step each time to get the
# middle of the link list
slow = head
fast = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
# slow point to the middle now
head2 = slow.next
# we cut of the linked list at middle
slow.next = None left = self.sortList(head)
right = self.sortList(head2)
return self.merge(left, right)
这里merge的时候建立了一个伪头结点,处理的时候就不用推断是否为第一个结点,尽管AC,但时间5500ms以上。而c++代码仅仅须要200多ms,差距还是比較大的
【leetcode】sort list(python)的更多相关文章
- 【leetcode】Word Break(python)
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【链表】Sort List(归并排序)
题目: Sort a linked list in O(n log n) time using constant space complexity. 思路: nlogn的排序有快速排序.归并排序.堆排 ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
随机推荐
- Java KeyStore 用命令生成keystore文件自己生成证书,简介
1.生成keyStore文件 在命令行下执行以下命令: Shell代码 收藏代码 keytool -genkey -validity 36000 -alias www.zlex.org -keyalg ...
- android解决AVD中文路径无法启动问题
在as中新建一个AVD,然而启动时却报错,总之是不能找到中文路径 然后这个虚拟设备被默认安装在了C盘我的用户李敏啊,而我用户名是中文名导致无法识别 解决办法,使用链接文件格式修改虚拟设备配置路径, 比 ...
- python图片处理(一)
在matlab中有相应的图像进行二值化处理,并且标记连通区域 L = bwlabel(BW,n) 返回一个和BW大小相同的L矩阵,包含了标记连BW中每个连通区域的类别标签,标签的值是1.2.num(连 ...
- Ajax请求中的async:false/true
Ajax请求中的async:false/trueasync. 默认是 true,即为异步方式,$.ajax执行后,会继续执行ajax后面的脚本,直到服务器端返回数据后,触发$.ajax里的succes ...
- 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 2.删除字段 ALTER TABLE table_NAME DROP CO ...
- Effective STL 学习笔记 Item 16:vector, string & C API
有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...
- 彻底卸载sql2008后重新安装
彻底卸载sql2008方法 --打开控制面板,在控制面板中卸载所有带sql server的程序. --删除C:\Program Files\Microsoft SQL Server这整个文件夹, -- ...
- 丑数(UVa136)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&a ...
- javascript 去除最后一个字符自定义的方法
//公共去除最后字符方法 function dtrim(str, s){ var reg = eval("/"+s+"$/gi"); str=str.repla ...
- 【LOJ】#2384. 「HNOI2013」切糕
题解 神仙网络流啊-- naive的我一直想把每个纵轴拆点,每个纵轴建R个点(大概是要跑费用流吧--)--然后第二个限制就gg了,什么也想不出来,菜啊TAT 后来我发现大神们的建图都是,一个原点,一个 ...