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的更多相关文章

  1. 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. 解 ...

  2. [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 ...

  3. [LeetCode] 88. Merge Sorted Array 合并有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  4. [leetcode 23]Merge k Sorted Lists

    1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...

  5. [Leetcode][021] Merge Two Sorted Lists (Java)

    题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 ...

  6. 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 ...

  7. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  8. LeetCode 56. Merge Intervals (合并区间)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  9. [LeetCode] Accounts Merge 账户合并

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  10. [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆

    转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...

随机推荐

  1. build spark

    Error : Failed to find Spark jars directory (/home/pl62716/spark-2.2.0-SNAPSHOT/assembly/target/scal ...

  2. mycat学习日记:全局sequence

    mycat分库分表的情况下,原生mysql的自增长主键无法满足主键全局唯一这个要求.看了MYCAT社区从零开始的一篇博客,加上自己的实践,大概总结一下. 目前mycat对于全局sequence主要提供 ...

  3. Hadoop体系结构

    在前面的博文中,我已经介绍过Hadoop的基本概念了(见博文初识Hadoop),今天来介绍一下Hadoop的体系结构. Hadoop的两大核心是HDFS和MapReduce,而整个Hadoop的体系结 ...

  4. spring assert 用法

    spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert ...

  5. .Net平台互操作技术:02. 技术介绍

    上一篇文章简单介绍了.Net平台互操作技术的面临的主要问题,以及主要的解决方案.本文将重点介绍使用相对较多的P/Invoke技术的实现:C#通过P/Invoke调用Native C++ Dll技术.C ...

  6. 用Python完成根据日期计算是星期几

    import datetime def week(year,month,day): someday=dayetime.date(year,month,day) result={ "0&quo ...

  7. robotframework介绍

    1.测试用例使用文本文件(TXT或者TSV文件)保存,使用制表符分隔数据.可以方便的使用任何文本编辑器,或者EXCEL编辑测试用例.也可以使用HTML格式创建用例.2.测试用例中支持变量使用,可以使用 ...

  8. SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇)

    SQLSERVER是怎麽通过索引和统计信息来找到目标数据的(第三篇) 最近真的没有什么精力写文章,天天加班,为了完成这个系列,硬着头皮上了 再看这篇文章之前请大家先看我之前写的第一篇和第二篇 第一篇: ...

  9. sqlserver创建触发器

    Create TRIGGER [dbo].[tr_Delete_AllocationedDN] --删除指定账户已分配未拣货的任务 ON [dbo].[FRU_PickLocationNew] --触 ...

  10. 轮播插件unslider.min.js使用demo

    有两种应用方式: 1.轮播图片作为<img>标签使用 HTML代码: <html> <head> <meta charset="utf-8" ...