88-Merge Sorted Array
来源:互联网 发布:中国地名数据库下载 编辑:程序博客网 时间:2024/05/21 06:31
题目
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.
分析
从后向前比较
//在当测试用例是[0],0,[1],1 时,会输出错误的结果········while(l1>=0&&l2>=0) nums1[l--] = nums1[l1] > nums2[l2] ? nums1[l1--] : nums2[l2--];
实现
class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int l = m + n-1, l1 = m-1 , l2 = n-1 ; while (l2 >= 0) nums1[l--] = l1 >= 0 && nums1[l1] > nums2[l2] ? nums1[l1--] : nums2[l2--]; }};
阅读全文
0 0
- [88]Merge Sorted Array
- 88 Merge Sorted Array
- 88 Merge Sorted Array
- #88 Merge Sorted Array
- 88Merge Sorted Array
- 88Merge Sorted Array
- [88]Merge Sorted Array
- 88Merge Sorted Array
- 88 Merge Sorted Array
- 88、Merge Sorted Array
- [88] Merge Sorted Array
- 88-Merge Sorted Array
- 88-Merge Sorted Array
- LeetCode(88) Merge Sorted Array
- [leetcode 88] Merge Sorted Array
- LeetCode[88]Merge Sorted Array
- leetcode-88 Merge Sorted Array
- leetcode || 88、Merge Sorted Array
- 51Nod 1046 A^B Mod C
- MongoDB--架构搭建 分片+副本集
- Codeforces Maximum in Table
- python脚本实现git commit hooks钩子
- 高性能 DOM & 重排重绘
- 88-Merge Sorted Array
- 【Docker】Centos7使用docker下安装MySQL
- 静态测试与动态测试
- 设计模式 (十)装饰模式(Decorator)
- 90-Subsets II
- 配置 MyEclipse中的JDK
- lua 元表学习笔记
- 最小的K个数 (最小堆)
- ArrayList与LinkedList区别