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.

ExamplInpunums1 = [1,2,3,0,0,0], m = 3

nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]

思路:
定义三个指针i,j,k,分别指向nums1,nums2,混合数组的末尾。
当nums2有剩余,继续循环,将剩余元素排入混合数组。
nums1有剩余,不用管,因为它本身包含在混合数组里。

注意:
第一个while

while (i >= 0 && j >= 0)

确保nums1/nums2其中一个数组都插进混合数组。不可以

while ( i != 0 && j !=0)

第二个while条件是 j >= 0,也是为了把所有元素插入。

代码:

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1; j = n - 1; k = m + n - 1;
while (i >= 0 && j >= 0){
if (nums1[i] >= nums2[j])
nums1[k--] = nums1[i--];
else nums1[k--] = nums2[j--];
}
while (j >= 0)
nums1[k--] = nums2[j--];
}
}

Leetcode88_Merge Sorted Array_Easy的更多相关文章

  1. [LeetCode] 88. Merge Sorted Array_Easy tag: Two Pointers

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

  2. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  3. Merge Sorted Array

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

  4. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  5. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

随机推荐

  1. notepad 正则表达式 复制 文本

  2. brctl 命令详解

    安装网桥管理工具包:bridge-utile ```# yum install bridge-utils -y``` ```使用brctl命令创建网桥br1```# brctl addbr br1`` ...

  3. Numpy 数组简单操作

    创建一个2*2的数组,计算对角线上元素的和 import numpy as np a = np.arange(4).reshape(2,2) print (a) #[[0 1] # [2 3]] n1 ...

  4. PDF文档导出

    代码如下: /// <summary> /// 获取html内容,转成PDF(注册) /// </summary> public void DownloadPDFByHTML( ...

  5. Redis慢查询日志学习功能

    慢查询日志 什么是SLOW LOG? Slow log是Redis用来记录查询执行时间超过给定时长的命令请求的日志系统.查询执行时间指的是不包括像客户端响应(talking).发送回复等IO操作,而单 ...

  6. Vue小案例 之 商品管理------修改商品数量以及增加入库日期属性

    实现修改商品的数量: 加入的代码: css: .clear-btn{ text-align: right; padding-right: 10px; } .table-warp a{ text-dec ...

  7. fjwc2019 D6T2 密文(trie+贪心)

    #194. 「2019冬令营提高组」密文 设$s[i]$表示前$i$个密文的异或和 容易发现,只要知道$s[0]~s[n](s[0]=0)$就可以知道每一位的值. 转化一下,就变成了在完全图上求最小生 ...

  8. 点击button后刷新了页面

    今天遇到一个特别奇怪的事,在页面中使用button标签,添加了点击事件onclic,点击的时候倒是执行了绑定的方法,但页面被刷新了! 什么鬼?我没与提交表单啊! 原来,button默认具有提交表单的动 ...

  9. mariadb的flashback到底怎么样???防误删可以,但算不上真正的闪回--再看mariadb 10.3的System-Versioned Tables

    mariadb 在10.2.4引入闪回特性,支持DML(INSERT, DELETE, UPDATE)操作的闪回,不支持DDL语句,使用闪回,必须设置binlog_row_image=FULL. 其原 ...

  10. c++ kafka 客户端rdkafka报Receive failed: Disconnected问题原因以及解决方法

    %3|1538976114.812|FAIL|rdkafka#producer-1| [thrd:kafka-server:9092/bootstrap]: kafka-server:9092/0: ...