【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 ...
随机推荐
- redis tutorail
命令 set get incr expire 秒 ttl -1 不会过期 list : lpush rpush lpop rpop lrange llen se ...
- windows系统 安装MongoDB
1.下载 官网下载地址:https://www.mongodb.com/download-center#community 2.配置MongoDB a.在e:\MongoDB(可随意起)下面建一个da ...
- virtualenv,virtualenvwrapper安装及使用
1.安装 # 安装: (sudo) pip install virtualenv virtualenvwrapper # centos7下 pip install virtualenv virtual ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
- CF293B. Distinct Paths
B. Distinct Paths time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Keras中RNN不定长输入的处理--padding and masking
在使用RNN based model处理序列的应用中,如果使用并行运算batch sample,我们几乎一定会遇到变长序列的问题. 通常解决变长的方法主要是将过长的序列截断,将过短序列用0补齐到一个固 ...
- TradingView 为 k 线柱添加标记
看别人翻译的开发文档: 开发文档地址:https://zlq4863947.gitbooks.io/tradingview/ getMarks(symbolInfo, startDate, endDa ...
- 20169211《Linux内核原理及分析》第十二周作业
Collabtive 系统 SQL 注入实验 实验介绍 SQL注入漏洞的代码注入技术,利用web应用程序和数据库服务器之间的接口.通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串, ...
- 虚拟机spark集群搭建
RDD弹性分布式数据集 (Resilient Distributed Dataset) RDD只读可分区,数据集可以缓存在内存中,在多次计算间重复利用. 弹性是指内存不够时可以与磁盘进行交互 join ...
- CodeForces 140C New Year Snowmen(堆)
题面 CodeForces 题解 因为要保证两两不同,所以不能单纯的开堆来维护,堆维护一个二元组,个数为第一关键字,编号为第二关键字,对于一个相同的颜色,统计一下这个颜色的个数再用堆来维护就好了. # ...