问题描述: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.

算法分析:合并两个数组,但是合并后的数组是原来的数组1,不能新建空间,假设数组1有足够的空间,从后往前遍历,避免覆盖问题。同类问题还有合并两个有序的链表,链表和数组的问题都相似。

//从后往前遍历比较,不会遇到覆盖的问题
public class MergeSortedArray
{
public void merge(int[] nums1, int m, int[] nums2, int n)
{
int i = m-1, j = n-1;
int z = m+n-1;
while(i >= 0 && j >= 0)
{
if(nums1[i] > nums2[j])
{
nums1[z--] = nums1[i--];
}
else
{
nums1[z--] = nums2[j--];
}
}
if(i < 0)
{
for(int k = j; k >= 0; k --)
{
nums1[z--] = nums2[k];
}
}
}
}

合并两个有序链表:

public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dumy = new ListNode(0);
ListNode head = dumy;
while(l1 != null && l2 != null)
{
if(l1.val < l2.val)
{
dumy.next = l1;
l1 = l1.next;
}
else
{
dumy.next = l2;
l2 = l2.next;
}
dumy = dumy.next;
}
if(l1 == null)
{
dumy.next = l2;
}
if(l2 == null)
{
dumy.next = l1;
}
return head.next;
}
}

MergeSortedArray,合并两个有序的数组的更多相关文章

  1. merge-sorted-array——合并两个有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  2. 合并两个有序链表(java实现)

    题目: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-&g ...

  3. Leecode刷题之旅-C语言/python-88合并两个有序数组

    /* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...

  4. 088 Merge Sorted Array 合并两个有序数组

    给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1中,使得 num1 成为一个有序数组.注意:你可以假设 nums1有足够的空间(空间大小大于或等于m + n)来保存 ...

  5. Leetcode 88:合并两个有序数组

    Leetcode链接 : https://leetcode-cn.com/problems/merge-sorted-array/ 问题描述: 给定两个有序整数数组 nums1 和 nums2,将 n ...

  6. 每日一道 LeetCode (19):合并两个有序数组

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  7. 合并两个有序数组a和b到c

    问题:两个有序数组a和b,合并成一个有序数组c. // 合并两个有序数组a和b到c void Merge_Array(int a[], int n, int b[], int m, int c[]) ...

  8. leetcode-只出现一次的数字合并两个有序数组

    题目:合并两个有序数组 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素 ...

  9. [Swift]LeetCode88. 合并两个有序数组 | Merge Sorted Array

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

随机推荐

  1. jQuery-实现图片的放大镜显示效果

    jQuery-实现图片的放大镜显示效果 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/word ...

  2. JavaScript-rem字体自适应

    给html标签上添加 id=“FontSize”: 你期望满屏的rem值,如: <html font-size:100px></html> 我的主要内容为1200px,那么我的 ...

  3. CodeForces 663A Rebus

    A. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  4. Linux磁盘分区的理解

    磁盘分割: 一块磁盘可以被分割为多个分区artition. 磁盘链接的方式 正常的实体机使用的都是/dev/sd[a-]的硬盘档名 虚拟机可能会使用/dev/vd[a-p]这种装置档名 SATA/US ...

  5. Python2 显示 unicode

    用户想要看的是 u'中文' 而不是 u'\u4e2d\u6587',但是在 Python2 中有时并不能实现. 转译 转义字符是这样一个字符,标志着在一个字符序列中出现在它之后的后续几个字符采取一种替 ...

  6. python生成百分数

    >>> a = float(5.69875) >>> b = float(8.49385) >>> print a/b 0.67092661160 ...

  7. 我的Android进阶之旅------>如何解决Android 5.0中出现的警告: Service Intent must be explicit:

    我的Android进阶之旅-->如何解决Android 5.0中出现的警告: java.lang.IllegalArgumentException: Service Intent must be ...

  8. SPL(Standard PHP Library 标准PHP类库)

    SplFileObject 读取大文件从第N行开始读: $line = 10; $splFileObj = new SplFileObject(__FILE__,'r'); $splFileObj-& ...

  9. OpenERP学习过程1

    系统为Win7 32位,下载并安装OpenERP: 1. 下载地址http://nightly.odoo.com/7.0/nightly/exe/ 2. 双击开始安装,由于选择的是all-in-one ...

  10. BLOG总结

    1.登录:http://www.cnblogs.com/shaojiafeng/p/7868195.html 2.注册 - urls -前端页面中写 username ,password,passwo ...