LeetCode题解——Merge Sorted Array
2015-07-21 21:12
295 查看
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 andnums2 are m and n respectively.
聪明的想法,从后往前比就不会覆盖掉原来未比较的数组元素。
4ms
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 andnums2 are m and n respectively.
聪明的想法,从后往前比就不会覆盖掉原来未比较的数组元素。
4ms
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { if(n==0) return; int i=m-1, j=n-1, k=m+n-1; while(i>=0 && j>=0) { if(nums1[i]>nums2[j]){ nums1[k--] = nums1[i--]; }else{ nums1[k--] = nums2[j--]; } } while(i>=0) { nums1[k--] = nums1[i--]; } while(j>=0) { nums1[k--] = nums2[j--]; } } };
相关文章推荐
- 二叉树 二叉树的性质 存储结构 遍历二叉树 C实现二叉树的创建和遍历 线索二叉树
- 39. Combination Sum
- Codeforces Gym 100463B Music Mess Hash 逻辑题
- [leedcode 105] Construct Binary Tree from Preorder and Inorder Traversal
- 00-自测4. Have Fun with Numbers (20)
- hdu5294||2015多校联合第一场1007 最短路+最大流
- ARC工程导人非ARC的代码
- 图像处理与计算机视觉基础,经典以及最近发展
- oracle取整操作
- TCP/IP协议
- 011.Singleton 单例模式
- 012.private protect public 访问控制符
- Python中choice用法与三元操作
- ./configure报错mv: cannot move './confsjjYiM/out' to 'cmake/Makefile': Permission denied
- BZOJ 1303: [CQOI2009]中位数图( )
- android LinearLayout设置selector不起作用解决
- LeetCode学习笔记
- 50个强大的Python模块
- UVa 10623 - Thinking Backward (平面图的欧拉公式)
- 00-自测3. 数组元素循环右移问题 (20)