[LeetCode] Merge Sorted Array
2015-07-02 18:11
344 查看
A classic subroutine of merge sort. Just merge the elements from back to forth. Keep a pointer for the merged position of the element and two other pointers for elements in nums1 and nums2 respectively.
The code is as follows.
The code is as follows.
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int p = m + n - 1, p1 = m - 1, p2 = n - 1; while (p1 >= 0 && p2 >= 0) { if (nums1[p1] >= nums2[p2]) nums1[p--] = nums1[p1--]; else nums1[p--] = nums2[p2--]; } while (p2 >= 0) nums1[p--] = nums2[p2--]; } };
相关文章推荐
- 博客地址
- C++ 让程序能够访问名称空间std的方法 以及 main函数头详解
- 2015.7.2
- 重构原则
- wpf,记录一下颜色设置的2中方法,,,
- php集成财付通支付接口
- 分治法、动态规划、贪心算法
- 如何在linux中启动scala程序 以及 命令行中启动java程序
- LoanJFrame
- 区别:Use MFC In A Shared DLL 和 Use MFC In A Static Library
- mongodb主从复制
- MarkDown基本使用方法
- MultipeerConnectivity应用-点对点连接
- java总结(之四)--类型信息
- Android 跨进程事件处理(一)——模拟点击、全局截屏
- AFNetworking2.0源码解析<三>
- AFNetworking2.0源码解析<二>
- WeifenLuo.WinFormsUI.Docking.dll使用
- 安装配置好openstack环境的虚拟机,需要修改ip时,在数据库中同步修改ip的方法
- MoneyJFrame