LeetCode Summary Sort

============================ Sort =================================


------------------------- Merge Sort --------------------------
88. Merge Sorted Array(合并2个有序数组):朴素


315. Count of Smaller Numbers After Self(给定一列数,返回每个数右边比它小的个数)
Discuss:在一个数右边比它小的,就是稳定排序时,从它的右边跳到左边的个数。


327. Count of Range Sum(给定一个数组以及一个范围,返回和在这个范围内的数组下标)
Discuss: Two pointers + merge sort


------------------------- Bucket Sort --------------------------
220. Contains Duplicate III(两数之差最多是t,下标距离最多是k)
把数都除以(t+1),归类到一个个桶里面。然后用map标记。
之差<=t,要么在一个桶,要么相邻桶。期间,要把距离跟i大于k的从map里面删除。
The idea is like the bucket sort algorithm. Suppose we have consecutive buckets covering 
the range of nums with each bucket a width of (t+1). If there are two item with difference <= t, 
one of the two will happen:
(1) the two in the same bucket
(2) the two in neighbor buckets
精简的python
for i in xrange(n):
m = nums[i] / w
if m in d:return True
if m - 1 in d and abs(nums[i] - d[m - 1]) < w:return True
if m + 1 in d and abs(nums[i] - d[m + 1]) < w:return True
d[m] = nums[i]
if i >= k: del d[nums[i - k] / w]


-------------------------------- Others ----------------------------
215. Kth Largest Element in an Array(找到第k大的数)
S1(Mine): 排序
324. Wiggle Sort II(一列数,按照高山低谷间隔排序)
S(Discuss): 
计算中间数,把比它小的放在一边,比它大的放在另一边;然后左边和右边穿插。直至最后。
164. Maximum Gap(无序数组,找到连续段之间最大间隔)
Mine1: 排序, ans = min(nums[i]-nums[i-1]);
Mine2: 使用map记录是否出现,遍历map求最大间距。
179. Largest Number(将一串数排序,是的组成的数最大)
Mine: 排序,然后连起来,我写的这个cmp真是太凋敝了!
static bool cmp(string a,string b)
{return a+b>b+a;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值