[leetcode] Merge Sorted Array
2015-06-07 19:07
295 查看
From : https://leetcode.com/problems/merge-sorted-array/
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 nums1and nums2 are m and n respectively.
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 nums1and nums2 are m and n respectively.
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int index = m+n-1, idx1 = m-1, idx2 = n-1; while(idx2 >= 0 && idx1 >= 0) { if(nums2[idx2] > nums1[idx1]) { nums1[index--] = nums2[idx2]; idx2--; } else { nums1[index--] = nums1[idx1]; idx1--; } } while(idx1>=0) { nums1[index--] = nums1[idx1--]; } while(idx2>=0) { nums1[index--] = nums2[idx2--]; } } };
相关文章推荐
- 英文不好,该怎么写论文
- 前端优化杂记
- web标签语义化
- 《梦断代码》随笔之三
- 如何使用Java程序通过JDBC访问HDInsight Hive Server
- 微信又出新玩法,继小视频后公众号文章支持添加音乐
- KAFKA分布式消息系统
- 最长公共子序列与最长上升子序列--再学DP
- Android入门——电话拨号器和四种点击事件
- 最长公共子序列与最长上升子序列--再学DP
- 第14周上机实践项目2——用文件保存的学生名单
- shell学习第一天
- 最适合你性格的职业类型是什么?
- 【BZOJ】【2435】【NOI2011】道路修建
- 有关国际化的东西
- 类模板相互引用的问题(错误:缺少类型说明符-假定为int。注意:C++不支持默认int)
- 单链表中重复元素的删除
- 周围人的影响与学习
- [leetcode][DP] House Robber II
- 本周周报