LeetCode - Merge Sorted Array
2013-12-27 02:08
344 查看
Merge Sorted Array
2013.12.27 01:18
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
Solution1:
First solution is simple and foolish enough, just put them together and sort it.
Time complexity is O((m + n) * log(m + n)), space complexity is O(1).
Accepted code:
Solution2:
Merge two arrays with O(m + n) extra space. Time complexity is O(m + n), space complexity is O(m + n). In-place merge seems more efficient, but somewhat a little tricky to understand. I'll put my in-place merge solution here when I grab the idea.
Time complexity is O(m + n), space complexity is O(m + n).
Accepted code:
2013.12.27 01:18
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
Solution1:
First solution is simple and foolish enough, just put them together and sort it.
Time complexity is O((m + n) * log(m + n)), space complexity is O(1).
Accepted code:
#include <algorithm> using namespace std; class Solution { public: void merge(int A[], int m, int B[], int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. for(int i = m; i < m + n; ++i){ A[i] = B[i - m]; } sort(A, A + m + n); } };
Solution2:
Merge two arrays with O(m + n) extra space. Time complexity is O(m + n), space complexity is O(m + n). In-place merge seems more efficient, but somewhat a little tricky to understand. I'll put my in-place merge solution here when I grab the idea.
Time complexity is O(m + n), space complexity is O(m + n).
Accepted code:
#include <cstdlib> using namespace std; class Solution { public: void merge(int A[], int m, int B[], int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int *C = nullptr; if(nullptr == A || nullptr == B || m < 0 || n <= 0){ return; } C = new(nothrow) int[m + n]; if(nullptr == C){ printf("Error: bad memory allocation."); exit(0); } int i, j, k; i = j = k = 0; while(i < m && j < n){ if(A[i] < B[j]){ C[k++] = A[i++]; }else{ C[k++] = B[j++]; } } while(i < m){ C[k++] = A[i++]; } while(j < n){ C[k++] = B[j++]; } memcpy(A, C, (m + n) * sizeof(A[0])); delete[] C; } };
相关文章推荐
- 更接近于现实的社交产品设计――社会化广告
- IOS图片压缩和扩展
- roman to integer
- 进程同步之信号量机制(pv操作)及三个经典同步问题
- Power BI的一些视频演示资源
- 张小龙演讲精华:产品经理的方法论与价值观
- Quotient Polynomial
- Power BI的一些视频演示资源
- kernel网址
- 跨越13开启14--猿猿感想
- ViewPager和ScrollView滑动冲突
- 黑马程序员_String类
- 用户访谈过程中遇到的几个失控经历
- Uni2D 入门 -- Atlas
- 黑马程序员--面向对象(二)static关键字、静态代码块、对象的初始化过程
- 公众号我也快看不下去了!
- Java 第六天 Spring Annotation 和其它
- 2013全国互联互通工作盘点,互联带宽扩容668G
- 我为何停止使用Spring
- Linux 的 /tmp 目錄變成 1MB 的 overflow 檔案系統如何解決