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

Note:
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 nums1and nums2 are m and n respectively.

 public class Solution {
public void merge(int A[], int m, int B[], int n) { int i=m-1;
int j=n-1;
int k=m+n-1; while(i>=0 && j>=0) {
A[k--] = A[i] > B[j] ? A[i--] : B[j--]; //!wrong!!! after ?, i,j value changed. A[i--] > B[j--] ? A[i] : B[j]
} while(j>=0) {
A[k--] = B[j--];
}
}
}
 public class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int[] newArr = new int[m+n];
int i = 0,j = 0,k = 0;
while(i < m || j< n)
{
int a = i < m ?nums1[i] :Integer.MAX_VALUE;
int b = j < n ?nums2[j] :Integer.MAX_VALUE;
if(a > b)
{
newArr[k++] = b;
j++;
}
else
{
newArr[k++] = a;
i++;
}
}
System.arraycopy(newArr, 0, nums1, 0, newArr.length);
}
}

Merge Sorted Array 合并数组并排序的更多相关文章

  1. [Leetcode] 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. [LeetCode] 88. Merge Sorted Array 合并有序数组

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

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

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

  4. LeetCode Merge Sorted Array 合并已排序的数组

    void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...

  5. 【easy】88. Merge Sorted Array 合并两个有序数组

    合并两个有序的list 把排序好的nums2插入nums1中,假设nums1这个vector的空间永远是够的 思路:倒序!! class Solution { public: void merge(v ...

  6. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

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

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

  8. Leetcode#88. Merge Sorted Array(合并两个有序数组)

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

  9. [LeetCode] Merge Sorted Array 混合插入有序数组

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

随机推荐

  1. Swift--集合类型 数组 字典 集合

    数组 1.创建一个数组 var someInts = [Int]()空数组 someInts = []清空 var threeDoubles = Array(repeating: 0.0, count ...

  2. SignalR2.0开发实例之——设置时间、后台其他地方使用集线器、使用自己的连接ID

    一.连接的生命周期设置: 如下: // 该值表示连接在超时之前保持打开状态的时间长度. //默认为110秒 GlobalHost.Configuration.ConnectionTimeout = T ...

  3. java下radomAccessFile文件写入读取

    package cn.stat.p2.demo; import java.io.FileNotFoundException; import java.io.IOException; import ja ...

  4. [BZOJ]3643 Phi的反函数

    我承认开这篇文章只是因为好笑…… 估计Zky神看见3737会很郁闷吧. http://www.lydsy.com/JudgeOnline/problem.php?id=3643 本来想直接交3737改 ...

  5. window.open || window.showModalDialog || window.showModelessDialog

    http://dwcmayday201204063551.iteye.com/blog/1621751 http://www.cnblogs.com/zhangyi85/archive/2009/09 ...

  6. C#进程间通信--API传递参数(SendMessage)

    原文 C#进程间通信--API传递参数(SendMessage)  我们不仅可以传递系统已经定义好的消息,还可以传递自定义的消息(只需要发送消息端和接收消息端对自定义的消息值统一即可).下面的发送和接 ...

  7. 玩Linux桌面发现一个最佳的组合配置

    其实前段时间玩Arch,其实不难,主要是太浪费时间配置折腾了,学到有用的东西太少,不能让我快速进入编程工作的状态,(真不知道有些人用Gentoo和Arch都能用出优越感了,就因为难安装和配置??)但是 ...

  8. Google GFS文件系统深入分析

    Google GFS文件系统深入分析 现在云计算渐成潮流,对大规模数据应用.可伸缩.高容错的分布式文件系统的需求日渐增长.Google根据自身的经验打造的这套针对大量廉价客户机的Google GFS文 ...

  9. linux 版本家族

    1. 简单的说,在桌面系统上,可分为Debian和RedHat两大分支,然后Debian这一分支到现在比较火的是Ubuntu, RedHat比较火的是Fedora.贴一下它们的版本历史:  fedor ...

  10. Delphi7下实现HTTP的Post操作 转

    Delphi7下实现HTTP的Post操作 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Gra ...