leetcode-88. Merge Sorted Array
2016-04-27 20:42
405 查看
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.
Subscribe to see which companies asked this question
思路:nums1的值整体往后移动n位,然后两个指针分别指着对应起点,依次比较小的从nums[0]开始放。
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.
Subscribe to see which companies asked this question
思路:nums1的值整体往后移动n位,然后两个指针分别指着对应起点,依次比较小的从nums[0]开始放。
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { //思路:nums1先整体向后移动n位,再从头开始两个指针一个一个比较排序 for(int i = m-1;i>=0;i--) { nums1[i+n] = nums1[i]; } int nums1Pointer = n; int nums2Pointer = 0; int index = 0; while(nums1Pointer < m+n && nums2Pointer < n) { if(nums1[nums1Pointer] < nums2[nums2Pointer]) { nums1[index++] = nums1[nums1Pointer++]; } else { nums1[index++] = nums2[nums2Pointer++]; } } while(nums1Pointer < m+n) { nums1[index++] = nums1[nums1Pointer++]; } while(nums2Pointer < n) { nums1[index++] = nums2[nums2Pointer++]; } } };
相关文章推荐
- [转]iphone不越狱抓包
- openCV中图像间的加减乘除运算
- 虚拟机安装VMware-tools
- 直接插入排序
- C++用new来创建对象和非new来创建对象的区别
- 数据结构 - 线性表的顺序实现
- servlet--百度百科
- 漫画城之搜索
- Exception
- FileChannel
- python格式化dict输出
- 1015. Reversible Primes (20)
- Best Coder Matrix(数学)
- Houdini中全景摄像机shader立体左右眼成像方法
- 主成分分析(Principal Component Analysis)
- 最短路问题(各种方法整理)
- Nginx--后端节点健康检查
- zabbix3.0 从坚持到用起 开始配置管理界面
- mysql中变量character_set_connection的具体作用
- Retrofit——Cookie