Sort
共 9 篇文章
Leetcode - First Missing Positive
将各个正数放入相应下标处,使之满足nums - 1] = nums*,则一遍扫描后值在数组长度范围内的都按序排列了,然后二遍扫描遇到第一个打破秩序的位置i,则 i + 1就是第一个缺失的正数。 解析清楚的Code Ganker博客: http://blog.csdn.net/linhuanmars/article/details/20884585 简洁的实现: https://leetcode.com/discuss/24013/my-short-c-solution-o-1-space-and-o-n-time public class Solution { public int firs…
Leetcode - Shortest Word Distance II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it? Design a class which receives a list of words in the constructor, and implements a met…
Leetcode - Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer…
2015-08-16 10:17 · Sort
Leetcode - Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a string instead of an integer. [分析] 贪心思想,任两个数字字符串a, b, 若 a + b > b + a, …
Leetcode - Insertion Sort List
Sort a linked list using insertion sort. 数组的插入排序通常做法是从当前元素往前遍历找到插入点,而单向链表无法回溯,于是每次从头开始寻找插入点。使用dummy额外节点技巧可使实现更简洁。 开始未将已排序部分和未排序部分分开,会死循环从而OutOfMemory,简单带入2->1 可看出bug。 public class Solution { public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) return head; ListN…
Leetcode - Sort List
Sort a linked list in O(n log n) time using constant space complexity. 将链表二分,递归排序子链表,然后merge 已排序的两子链表。 public class Solution { public ListNode sortList(ListNode head) { if (head == null || head.next == null) return head; // split the list in a 2 pointer style ListNode p = head, q = head; while (q.n…
2015-08-15 17:41 · Divide and Conque · Sort
Leetcode - Meeting Rooms II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. For example, Given [[0, 30],[5, 10],[15, 20]], return 2. [分析] 初始思路,使用一个变量availableTime表示当前所有已安排会议的最早结束时间,对于每个待安排会议,比较其start 和 availableTi…
2015-08-15 16:28 · Sort
Leetcode - Insert Interval
[分析] 这道题思路直接,但bug free需要费些心力。第一版未优化的代码笔记长,未贴。 下面给出的两个实现,实现1是使用二分法找到新区间应插入的位置,然后检查是否需要和上一个区间合并(容易忽略),最后同后面的区间逐个检查是否需要合并。无需额外空间,Code Ganker 指出这种做法严格上非O(N),因为合并时有数组remove操作,最坏情况下是O(N^2),但跑leetcode测试用例这种实现比实现2效率高。 实现2:代码非常简洁,需要使用额外空间。先跳过新区间无交集且在新区间前面的,然后merge 和新区间有交集的,最后加上剩余的。 [ref] [url]http://blog.cs…
2015-08-14 10:12 · Array · Sort · Binary Search
Leetcode - Merge Intervals
Given a collection of intervals, merge all overlapping intervals. [分析] 这题思路不难,将输入的区间数组按左边界排序然后合并。但是调试了很久,收获是熟悉了些Java语法。首先,忘记对ArrayList排序可以使用Collections.sort(),不厌其烦地在ArrayList和数组间转换为了能使用Arrays.sort进行排序;然后使用Iterator对Arrays.asList()转化得到的ArrayList进行删除操作时遭遇了java.lang.UnsupportedOperationException, Googl…
2015-07-12 09:45 · Sort