给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 使得 num1 成为一个有序数组。

说明:

  • 初始化 nums1 和 nums2 的元素数量分别为 m 和 n
  • 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。

示例:

输入:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3 输出: [1,2,2,3,5,6]
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m-1;
int j = n-1;
int k = m+n-1;
while (i>=0 && j>=0) {
nums1[k--] = nums1[i] >= nums2[j]?nums1[i--]:nums2[j--];
}
while (j>=0) {
nums1[k--] = nums2[j--];
}
}
}

LeetCode88.合并两个有序数组的更多相关文章

  1. leetcode-88合并两个有序数组

    合并两个有序数组 思路:利用索引合并两个列表,排序.注意不需要返回值,只修改nums1 class Solution: def merge(self, nums1: List[int], m: int ...

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

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

  3. leetcode-88. 合并两个有序数组 · Aaray

    题面 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 样例 1. 输入: nums1 = [1,2,3,0,0,0], m ...

  4. 【leetcode-88,21】 合并两个有序数组/链表

    合并两个有序数组 (easy,1过) 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nu ...

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

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

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

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

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

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

  8. python3实现合并两个有序数组

    很早就听同学和师兄经常说刷题很重要,然而编程能力一直都很渣的我最近才开始从leetcode的初级算法开始.今天遇到的这道题虽然很简单,因为是头一次用自己的方法速度还不错,特此记录一下,还大神们请不要嘲 ...

  9. Leetcode 88 合并两个有序数组 Python

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

随机推荐

  1. 基于cdh5.10.x hadoop版本的apache源码编译安装spark

    参考文档:http://spark.apache.org/docs/1.6.0/building-spark.html spark安装需要选择源码编译方式进行安装部署,cdh5.10.0提供默认的二进 ...

  2. shell脚本之分析oracle数据库数据泵日志中表的大小

    1.分析日志格式如下 . . imported "xxx_330508"."xxx_T_DATA" 46.17 MB 268 rows . . imported ...

  3. Linux下无法清空回收站

    清空回收站发生错误,直接使用命令 sudo rm -rf ~/.local/share/Trash/files/* 搞定

  4. [hyperscan] hyperscan 1到1.5 --!!

    [hyperscan][pkg-config] hyperscan 从0到1路线图 接续前文,继续深入理解: 概述: 1.  自动机理论,是hyperscan的理论基础. https://zh.wik ...

  5. Python3+Selenium环境配置

    一.所需工具 1.Python3.6安装包 2.Selenium安装包(selenium-server-standalone-3.8),如果是Python3的话可以不用下载selenium压缩包,Py ...

  6. java web filter读取classpath配置文件内容

    以下demo,从类路径classpath中获取venus.properties(本项目中用到的文件),思路是在初始化的时候读取,然后放在局部变量里面. package club.codeapes.we ...

  7. MovieLens电影数据分析

    下载数据包 链接:https://grouplens.org/datasets/movielens/1m/ 解压: 四个文件分别是数据介绍,电影数据表,电影评分表,用户表 进行电影数据分析 进入ipy ...

  8. java 线程(六)死锁

    package cn.sasa.demo4; public class ThreadDemo { public static void main(String[] args){ DeadLockRun ...

  9. centos中文语言安装

    1.查看当前使用的系统语言 #echo LANG 2.查看系统是否安装中文 #locale 如有zh_cn,表示已经安装了中文语言 3.安装中文 #yum groupinstall chinese-s ...

  10. linux服务器性能查看

    1.1 cpu性能查看 1.查看物理cpu个数: cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l 2.查看每个物理cpu ...