88. Merge Sorted Array

Easy

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

Note:

  • The number of elements initialized in nums1 and nums2 are m and n respectively.
  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
package leetcode.easy;

public class MergeSortedArray {
@org.junit.Test
public void test() {
int[] nums1 = { 1, 2, 3, 0, 0, 0 };
int m = 3;
int[] nums2 = { 2, 5, 6 };
int n = 3;
print(nums1);
print(nums2);
merge(nums1, m, nums2, n);
print(nums1);
} public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i = m, j = 0; i < nums1.length && j < nums2.length; i++, j++) {
nums1[i] = nums2[j];
}
sort(nums1, m + n);
} private static void sort(int[] nums, int m) {
for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < m - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
} private static void print(int[] nums) {
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
System.out.println();
}
}

LeetCode_88. 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 tha ...

  2. 43. Merge Sorted Array && LRU Cache

    Merge Sorted Array OJ: https://oj.leetcode.com/problems/merge-sorted-array/ Given two sorted integer ...

  3. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

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

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

  5. 【LeetCode】88. Merge Sorted Array (2 solutions)

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  6. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

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

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

  8. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

  9. Merge Sorted Array

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

随机推荐

  1. 为什么要设置HTTP timeout?

    先看一个不设置timeout造成的线上事故. 一次线上事故 有一次生产上的一个服务出了点故障,一个原本每5分钟执行一次的定时任务突然不执行了.第一反应是任务执行报错,查看日志,却没有找到任何异常报错信 ...

  2. m_atoi

    自己实现atoi函数 函数定义:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时('\0')才结束转化,并将结 ...

  3. (一)AppScan的安装及破解

    IBM AppScan是一款目前最好用的Web 应用安全测试工具,Rational AppScan 可自动化 Web 应用的安全漏洞评估工作,能扫描和检测所有常见的 Web 应用安全漏洞,例如 SQL ...

  4. Python+request 使用pymysql连接数据库mysql的操作《十》

    使用指南.pymysql支持python2.7同时也支持python3.x.当前我用的是python2.7.所以过断选择了pymysql的使用,这里注意几点.一般我们连接数据库为了安全起见,都会要求按 ...

  5. VS2010 insert Oracle数据库

    背景:批量插入上万条数据到Oracle数据库的一张表里. 工具:VS2010. 因为是访问远程数据库,所以需要先装一个oracle client. 使用oracle客户端的方式访问数据库,需要添加对其 ...

  6. Codeforces Round #533 (Div. 2) E. Helping Hiasat(最大独立集)

    题目链接:https://codeforces.com/contest/1105/problem/E 题意:有 n 个事件,op = 1 表示我可以修改昵称,op = 2 表示一个名为 s_i 的朋友 ...

  7. python - django (母版与组件)

    # 把多个页面通用的部分提取出来 放在一个母版中 # 其它的页面只需要继承 母版就可以 # 使用步骤:( 继承的语句要放在第一行 ) """ 1. 把公用的 HTML 部 ...

  8. ADB命令使用详解

    ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 1.连接android设置 adb connect 设备名 例如: adb connect 12 ...

  9. ES6学习记录(一)

    Class类 Class的静态方法 类相当于实例的原型,所有在类中定义的方法,都会被实例继承.如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态 ...

  10. sublime 分屏显示 不是插件

    点击 view--layout --- 选择几屏即可(single / columns 2 ....) 快捷键  Alt + Shift + 1/2/3/4  分别对应1 ,2,3,4屏 如何把一个文 ...