这道题比较直接的想法就是用Merge
Sorted Array这个题的方法把两个有序数组合并,当合并到第(m+n)/2个元素的时候返回那个数即可,而且不用把结果数组存起来。算法时间复杂度是O(m+n),空间复杂度是O(1)。因为代码比较简单,就不写出来了,跟Merge
Sorted Array比较类似,大家可以参照这个题目的解法。
接下来我们考虑有没有优化的算法。优化的思想来源于orde...
分类:
其他好文 时间:
2015-04-02 09:08:26
阅读次数:
231
Insert Interval解法:
先用start,end两个变量来定位 newinterval可能要插入的起始点和结束点。 分三种情况来看是否需要调整或合并(merge)。
case1: 在start 之前直接copy 到结果res; case2: 在end之后也是直接copy到res中; case3: 需要合并的就是newInterval.start, newInterval.end, 和任选的一个interval 的interval.start , interval.end 四个边界的比较。 下...
分类:
其他好文 时间:
2015-04-02 06:41:07
阅读次数:
128
最近申请了微软的暑假实习,4号就要在线笔试了,在线测试系统用的是http://hihocoder.com/,今天试手做了一道题。
【题目】
原题链接:http://hihocoder.com/contest/hiho39/problem/1
输入
第1行:1个整数N,表示数组长度。
第2行:N个整数,表示数组的元素a[i],1≤a[i]≤2^31-1。
输出
第1行...
分类:
编程语言 时间:
2015-04-02 01:21:08
阅读次数:
169
Merge Two Sorted Lists
Merge
two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
解题思路:
这道题是我做的leetcode最容易的题目了...
分类:
其他好文 时间:
2015-04-01 23:47:10
阅读次数:
173
在忙项目的空隙看算法导论这本书,首先当然是接触一些排序算法,bubble sort,insert sort , merge sort , quick sort , heap sort,按着书上给的算法一一的实现上述算法。然后接着看哈希表,还有树,图,发现有些费劲。可能算法导论这本书对于初学者还是有....
分类:
编程语言 时间:
2015-04-01 19:48:16
阅读次数:
141
定义:
Redis is an open source, BSD licensed, advanced key-value
cache and store.
It is often referred to as a data structure server since
keys can contain strings, hashes, lists, sets,sorted
se...
分类:
其他好文 时间:
2015-04-01 17:52:18
阅读次数:
224
problem:
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
Hide Tags
Array Sort
题意:给...
分类:
其他好文 时间:
2015-04-01 17:50:17
阅读次数:
155
//---------------------------15/04/01----------------------------
//inplace_merge(要求有序)
templateclass BidirectionalIterator>
inline void inplace_merge(BidirectionalIterator first,
...
分类:
编程语言 时间:
2015-04-01 17:49:53
阅读次数:
131
problem:
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to the...
分类:
其他好文 时间:
2015-04-01 17:47:52
阅读次数:
162
/输入给定数组和长度,进行排序。/代码还没测试过,如果有朋友帮我找到了BUG,不胜感激。 1 #define Elmt int 2 3 void merge_sort(Elmt* sqc, int len) { 4 Elmt t; 5 Elmt *temp; 6 int i...
分类:
编程语言 时间:
2015-04-01 13:15:16
阅读次数:
113