[leetcode-88]Merge Sorted Array(C语言)
2015-07-11 21:57
609 查看
问题描述:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
代码如下:0ms
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
代码如下:0ms
[code]void merge(int* nums1, int m, int* nums2, int n) { int *tmp = (int *)malloc(sizeof(int)*(m+n)); int nums1Index=0,nums2Index=0,tmpIndex = 0; while(nums1Index<m && nums2Index<n){ if(nums1[nums1Index]<nums2[nums2Index]){ tmp[tmpIndex++] = nums1[nums1Index++]; }else{ tmp[tmpIndex++] = nums2[nums2Index++]; } } int i; if(nums1Index==m){ for(i = nums2Index;i<n;i++) tmp[tmpIndex++] = nums2[i]; } else if(nums2Index == n){ for(i = nums1Index;i<m;i++) tmp[tmpIndex++] = nums1[i]; } for(i = 0;i<m+n;i++) nums1[i] = tmp[i]; free(tmp); }
相关文章推荐
- C语言—判断IP地址是否合法
- 【学习笔记】【OC语言】创建对象
- C++中Exit()与atexit()函数的使用
- [leetcode-28]implement strstr()(C语言)
- hdoj5280最大区间和
- 【学习笔记】【OC语言】面向对象思想
- C语言及程序设计 实践项目――C语言程序初体验
- C语言及程序设计 例程 - C语言程序初体验
- leetcode 日经贴,Cpp code -Lowest Common Ancestor of a Binary Search Tree
- c++内存管理
- C语言结构体(一)
- C++ Primer Plus 第2章 编程练习
- c语言
- C++完美实现Singleton模式
- 【C++】静态成员与单例模式
- C++中的inline函数
- C语言的复习
- C++11新增for循环遍历方法
- 拆除vs发展c++程序开发过程中产生的.ipch和.sdf文件的方法
- C++11变参模板