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. APIClound 弹出层 Frame

    JS api.openFrame({ name: 'showPic', url: './showPic.html', rect: { // x: api.pageParam.marginBottom, ...

  2. 深入理解HashMap+ConcurrentHashMap的扩容策略

    前言 理解HashMap和ConcurrentHashMap的重点在于: (1)理解HashMap的数据结构的设计和实现思路 (2)在(1)的基础上,理解ConcurrentHashMap的并发安全的 ...

  3. C++中虚函数的作用

    一, 什么是虚函数(如果不知道虚函数为何物,但有急切的想知道,那你就应该从这里开始) 简单地说,那些被virtual关键字修饰的成员函数,就是虚函数.虚函数的作用,用专业术语来解释就是实现多态性(Po ...

  4. c++ STL中的next_permutation

    default (1) template <class BidirectionalIterator> bool next_permutation (BidirectionalIterato ...

  5. 一键PHP/JAVA安装工具

    OneinStack是一键PHP/JAVA安装脚本工具,包含lnmp,lamp,lnmpa,ltmp,lnmh,MySQL,PostgreSQL,MongoDB等 建议使用 PHP7.1+MYSQL5 ...

  6. nvgre

    GRE RFC2784 工作原理 Structure of a GRE Encapsulated Packet A GRE encapsulated packet has the form: ---- ...

  7. GDI的 点 线 面 双缓冲 位图的绘制

    1.输出文本 // 输出文本 ,,)); //设置字体颜色,但最后都要返回原来的字体格式 COLORREF clrBackColor = SetBkColor(hDC, RGB(,,)); //设置背 ...

  8. [c/c++] programming之路(6)、ASCII码,数据类型、随机数、字符转换及拼接等

    一.变量 #include<stdio.h> #include<stdlib.h> void main0(){ //数据使用必须在范围内,否则产生溢出 unsigned +;/ ...

  9. Codeforces 833A The Meaningless Game - 数论 - 牛顿迭代法 - 二分法

    Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. T ...

  10. Lottie的json动画

    我们的设计如是说: Lottie的json动画,优缺点有 优点: 1.开发成本低,动画的还原度高.设计师导出 json 文件后,交付开发.原本要1天甚至更久的动画实现,现在只要不到一小时甚至更少时间了 ...