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. ruby Iconv.iconv编码方法

    #定义一个UTF-8=>GBK的方法def encoding inStr    Iconv.iconv("GBK","UTF-8",inStr)end#定 ...

  2. Jquery使用ajax参数详解

    记录一下  Jquery使用ajax(post.get及参数详解) 1.get: $.ajax({ type: "GET", url: baseUrl + "Showco ...

  3. WinForm 开发框架 Jade UI Beta

    Jade UI Demo Beta 个人网站:http://www.2to.net 开源地址:https://github.com/dcdlove/JadeUI 预览DEMO下载: http://pa ...

  4. 老技术,UrlRewriter实现全站伪静态

    看人家做淘宝客很火,就做了个网站.seo的话当然需要全站伪静态了,问了下空间商不支持mvc,尼玛,好吧,isapi_rewrite支持吗?“额,不支持!” -_-! 额,好吧,搬出n年前的东西了:微软 ...

  5. Java基本语法和变量

    1基本语法 1.1 标识符.关键字 在程序中用于定义名称的都为标识符,如文件名称.类名称.方法名称或变量名称等. 在Java中标识符的定义格式由字母.数字._(下划线),$所组成,不能以数字开头, 不 ...

  6. Servlet之sendRedirect和getRequestDispatch

    Servlet的请求重定向和请求转发方法的比较分析: 1.getRequestDispatch是属于httpServletRequest对象的方法,请求转发是在同一个请求中完成的,因此整个过程只包含一 ...

  7. 我们为什么要看《超实用的HTML代码段》

    不知道自己HTML水平如何,不知道HTML5如何进化?看这张图 如果一半以上的你都不会,必须看这本书,阿里一线工程师用代码和功能页面来告诉你每一个技术点. 都会一点,但不知道如何检验自己,看看本书提供 ...

  8. 本号讯 | 微软被 Forrester 评为销售服务自动化解决方案领导者

    2017年第二季度独立研究机构 Forrester Research 在最新发布的 The Forrester Wave™: 销售服务自动化(Sales Force Automation Soluti ...

  9. 如何用Windows PowerShell替换命令提示符

    在Windows 10的"开始"按钮中将PowerShell替换为命令提示符,这不是很好吗?我知道你会有疑问,为什么要这样做?可能会失去了运行DOS命令的能力.好吧,让我解释一下. ...

  10. Jmeter进行接口的性能测试

    一.录制Jmeter脚本 录制Jmeter脚本有两种方法,一种是设置代理:一种则是利用badboy软件,badboy软件支持导出jmx脚本. 这里我们介绍第二种方法,利用badboy录制脚本,然后导出 ...