leetcode-19-merge
88. Merge Sorted Array

解题思路:
需要注意,两个数组是排好序的,且nums1够大。所以从两个数组的尾端开始比较,大的那个放在nums1的尾部,并且放了之后就可以前进。
例如nums1[i]<nums2[j],那么nums1尾部放nums2[j],然后j--。需要注意的是i可能小于0,这时候,需要用nums2[j]的值填充。
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m - 1;
int j = n - 1;
int index = m + n - 1;
while (j >= 0) {
if (i < 0)
nums1[index--] = nums2[j--];
else
nums1[index--] = nums1[i] > nums2[j] ? nums1[i--]:nums2[j--];
}
}
21. Merge Two Sorted Lists
leetcode-19-merge的更多相关文章
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- [LeetCode] 21. Merge Two Sorted Lists 合并有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- [leetcode 23]Merge k Sorted Lists
1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- [Leetcode][021] Merge Two Sorted Lists (Java)
题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 ...
- LeetCode 617. Merge Two Binary Tree (合并两个二叉树)
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- LeetCode 88. Merge Sorted Array(合并有序数组)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆
转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...
随机推荐
- 040 Combination Sum II 组合总和 II
给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合.C 中的每个数字只能在组合中使用一次.注意: 所有数字(包括目标)都是正整数. 解决方案集不 ...
- 2017 ACM Arabella Collegiate Programming Contest div2的题,部分题目写个题解
F. Monkeying Around 维护点在多少个线段上 http://codeforces.com/gym/101350/problem/F 题意:有m个笑话,每个笑话的区间是[L, R], ...
- Corn 表达式
CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...
- Eclipse Debug模式和断点调试
1行号上双击,打断点:再双击,取消断点.一般想调试哪一句代码,就在哪一句和下一句打上断点. 2在要执行的class文件上(有main方法的),右键--Debug As 然后程序正常走,当走到断点时,会 ...
- 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)
题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...
- JS计算24节气的方法
function getjq(yyyy,mm,dd){ mm = mm-1; var sTermInfo = new Array(0,21208,42467,63836,85337,107014,12 ...
- Linux下软件安装的四种方式
一.源码安装 步骤: 下载,解压源码(常见的源码打包格式:.tar.gz/.tar.bz2); 可以直接下载源码再上传至linux服务器,或者在联网状态下,直接通过wget等命令获取源码安装包;源码解 ...
- JFinal视频教程
最近开始录制JFinal视频教程,发布在腾讯课堂上,免费公开面向JFinal开发者,作为JFinal开发者入门学习.实际项目用遇到问题寻找解决方案的最好途径. 目前JFinal课程已经开始更新. 腾讯 ...
- AES加密示例
最近用到对文本内容进行加密,于是查了一下常用的加密算法: DES(Data Encryption Standard):对称算法,数据加密标准,速度较快,适用于加密大量数据的场合:3DES(Triple ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...