【LeetCode】88 - Merge Sorted Array
2015-08-12 23:17
363 查看
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.
Solution:
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.
Solution:
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { //runtime: 4ms int i=m-1; int j=n-1; int k = m+n-1; while(i >=0 && j>=0) { if(nums1[i] > nums2[j]) nums1[k--] = nums1[i--]; else nums1[k--] = nums2[j--]; } while(j>=0) nums1[k--] = nums2[j--]; } };
相关文章推荐
- socket_tcp
- EditText 里的属性
- 18种和“距离(distance)”、“相似度(similarity)”相关的量的小结
- leetcode--backtracking[0]
- HDU 3697贪心
- 打印python的堆栈stack
- 编写高质量代码改善C#程序的157个建议——建议15: 使用dynamic来简化反射实现
- intent.setFlags的参数含义及使用方法
- 如何愉快地接手一个项目
- [LeetCode.DP]Word Break ||
- POJ 1966--Cable TV Network 【求无向图的点连通度 构造最大流模型 && dinic】
- Visual Studio 2008 下安装boost的详细步骤 此博文包含图片
- 00 AppCan入门学习之关于AppCan的学习
- 黑马程序员——Java中对象的继承
- 构建施耐德楼控系统数据库后台服务器示例工程五(JAVA动态生成类)
- hdu 5379 Mahjong tree(树形dp)
- Mac OS上反编译android app的环境搭建
- Promise in AngularJS
- Qt中怎样把TXT文件中的数读入数组中
- arp与免费arp的区别,arp老化