leetcode 088 —— Merge Sorted Array
2015-08-05 16:43
423 查看
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.
思路:归并排序
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for (int i =0; i < m; i++)
nums1[n +m-1-i] = nums1[m-1-i];
cout << endl;
int p1 = 0;
int p2 = 0;
int p3 = 0;
while (p1 < m&&p2 < n){
if (nums1[p1+n] < nums2[p2])
nums1[p3++] = nums1[p1++ + n];
else
nums1[p3++] = nums2[p2++];
}
while (p1<m)
nums1[p3++] = nums1[p1++ + n];
while (p2 < n)
nums1[p3++] = nums2[p2++];
}
};
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.
思路:归并排序
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for (int i =0; i < m; i++)
nums1[n +m-1-i] = nums1[m-1-i];
cout << endl;
int p1 = 0;
int p2 = 0;
int p3 = 0;
while (p1 < m&&p2 < n){
if (nums1[p1+n] < nums2[p2])
nums1[p3++] = nums1[p1++ + n];
else
nums1[p3++] = nums2[p2++];
}
while (p1<m)
nums1[p3++] = nums1[p1++ + n];
while (p2 < n)
nums1[p3++] = nums2[p2++];
}
};
相关文章推荐
- Spring学习8-Spring事务管理(注解式声明事务管理)
- 为什么Java byte 类型的取值范围是-128~127
- GdiplusFlat(6)绘制文本
- Android Fragment 真正的完全解析(上)
- 哈夫曼编码 (Huffman code)的实现,压缩、解压缩
- 栈的实现
- fragmentActivity布局文件报错Error inflating class fragment
- NLP超级初步
- spring 配置文件属性
- CentOS 修改主机名
- JS学习之this关键字
- vs执行程序时提示无法启动程序,系统找不到指定的文件
- 轻松入门学习java入门的常用语言知识
- duilib中,子xml创建自定义控件
- Android.mk简介
- SYN Cookie的原理和实现
- 速度之王 — LZ4压缩算法
- Eclipse+Spring+SpringMVC+Mybatis+Maven+多库配置+resin
- UVa 725----Division(枚举)
- ROS 教程之 vision : 用各种摄像头获取图像