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. JS代码运行延迟

    还是上篇文章的项目. 现在是屏幕上需要显示九张图表,刚好用一张3X3的表格来显示.但是负责这块内容的同事始终没法让九张图表同时显示,有些图表的位置空了出来. 大家百思不得其解,最后只得求助技术经理. ...

  2. 后台任务利器之Hangfire

    后台任务利器之Hangfire 一.简述 Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的DI管理着生命周期,免去了在NF4 ...

  3. 故障案例:主从同步报错Fatal error: The slave I/O thread stops because master and slave have equal MySQL server

    https://blog.csdn.net/cug_jiang126com/article/details/46846031

  4. mysql非常全的和完整的总结

    (1)数据类型 类型 备注 tinyint/smallint/mediumint/int/bigint 1B/2B/3B/4B/8B float/double 单精度/双精度浮点型 decimal 不 ...

  5. JAVA基础之Properties类、序列化流及打印流、commons-IO

    个人理解: Properties类是个存储String类型的键值对的集合类,可以用其存储一些关键的账号密码什么的,同时后面的注释可以很好的帮助理解,但是需要注意的是其文件中不能出现其他的符号:序列化与 ...

  6. 2、HTTP状态码

    HTTP状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应 ...

  7. Vue2.0 官方文档学习笔记

    VUE2.0官方文档 基础部分: 1.VUE简介 Vue是一个基于MVVM的框架,其中M代表数据处理层,V代表视图层即我们在Vue组件中的html部分,VM即M和V的结合层,处理M层相应的逻辑数据,在 ...

  8. VC中包含的头文件名不区分大小写

    VC中包含的头文件名,不区分大小写如 #include "my.h" = #include "MY.H".

  9. 什么是Office Online Server和SharePoint 2016

    Microsoft Office Online Server是Microsoft Office Web Apps(OWA)服务器的下一个版本,最初于2012年发布.,可以下载Office Online ...

  10. BZOJ 2119: 股市的预测 SA

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 434  Solved: 200[Submit][Status][Discuss ...