Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

题目:

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 nums1 and nums2 are m and n respectively.

翻译:

给定2个排序好的整数数组nums1和nums2,把nums2合并到nums1中成为1个排序的数组。

提示:你可以假定nums1有足够的空间(大小>=m+n)来容纳来自nums2的额外的元素。nums1和nums2的元素的个数各自被初始化为m和n。

分析:

1. 如果nums1从前往后遍历的话,nums2中的元素需要插入nums1,这个时候每插入一次,就会需要将nums1的元素往后移动(或者需要申请额外的存储空间)。但是我们反过来想,由于合并后的数组长度是确定的,我们可以从最大的数开始写入,这个时候由于nums1的后面部分的空间是未使用的,刚好可以直接覆写。

2. 需要考虑比较特殊的情况,就是数组可能为空。2个数组都为空自然进不去循环。但是其中一个为空,就要考虑数组可能发生越界的情况了。

Java版代码(时间复杂度O(m+n)):

public class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m-1;
        int p2 = n-1;
        for(int i = m+n-1;i >= 0;i--){
            if(p2 < 0 || (p1 >= 0 && nums1[p1] > nums2[p2])){
                nums1[i]=nums1[p1];
                p1--;
            }else{
                nums1[i]=nums2[p2];
                p2--;
            }
        }
    }
}
时间: 2024-11-09 10:38:04

Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]的相关文章

Leet Code OJ 118. Pascal&#39;s Triangle [Difficulty: Easy]

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

[LeedCode OJ]#88 Merge Sorted Array

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/merge-sorted-array/ 题意: 给定两个已经排好序的数组,然后要你把第二个数组合并到第一个去 思路: 先用两个标记指向两个数组的第一个,然后一边比较,取小的放入新数组,一直移动标记直到最后去玩 class Solution { public: void merge(vector<int>& a,

Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

题目: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 翻译: 给定一个整数数组和一个整数k.找出是否存在下标i,j使得nums[i] = nums[j].同一时候i

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

leetCode 88. Merge Sorted Array 有序数组

88. Merge Sorted Array 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 n

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

88. Merge Sorted Array 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 n

No.88 Merge Sorted Array

No.88 Merge Sorted Array 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

Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array

88. Merge Sorted Array (Array)

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 init