关闭

LeetCode 88. Merge Sorted Array 归并的过程

标签: leetcode归并排序合并
324人阅读 评论(0) 收藏 举报
分类:

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


题意:对于有序的两个数组nums1,nums2进行归并到nums1里保持其有序性

1.按照归并排序进行归并的过程,需要单独开辟一块空间O(n),时间复杂度O(n)

2.在进行归并的过程,需要注意越界的情况,当其中之一的数组已经归并完毕,剩余的元素就是另外一个数组的元素

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        vector<int> temp1 = nums1;   
        for(int i=0,j=0,k=0;i<m+n;i++)
        {
            if(j>=m)
            {
                nums1[i] = nums2[k];
                k++;
            }
            else if(k>=n)
            {
                nums1[i]=temp1[j];
                j++;
            }
            else if(temp1[j]<=nums2[k])
            {
                nums1[i]=temp1[j];
                j++;
            }
            else
            {
                nums1[i]=nums2[k];
                k++;
            }
            
        }
    }
};

优化1:

1.上述代码使用了空间复杂度O(n),是因为忽略了一个条件,nums1的长度为(n+m)

2.从后往前赋值,比较归并

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int i = m - 1, j = n - 1, tar = m + n - 1;
	    while (j >= 0) {
    		nums1[tar--] = i >= 0 && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
	    }
    }
};


0
0
查看评论

Leetcode_88_Merge Sorted Array

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41631609 通过本文你可能学到的知识为: (1)当我们遇到问题从正面不能或者无法解决时,不要钻进去了出不来,而是静下心来,考虑能否从其反面出发来解决问题。...
  • pistolove
  • pistolove
  • 2014-11-30 23:37
  • 1970

【leetcode】88. Merge Sorted Array(Python & C++)

88. Merge Sorted Array题目链接88.1 题目描述:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume ...
  • liuxiao214
  • liuxiao214
  • 2017-09-05 21:39
  • 208

Leetcode:88. Merge Sorted Array(JAVA)

【问题描述】 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: ...
  • u010183658
  • u010183658
  • 2016-03-13 09:49
  • 987

leetcode 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 th...
  • wangyaninglm
  • wangyaninglm
  • 2015-06-24 22:47
  • 2948

leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may as...
  • xygy8860
  • xygy8860
  • 2015-07-25 09:45
  • 1020

LeetCode(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 ...
  • fly_yr
  • fly_yr
  • 2015-08-20 17:33
  • 1942

leetcode解题之88 #Merge Sorted Array Java版

leetcode解题之88 #Merge Sorted Array Java版
  • mine_song
  • mine_song
  • 2017-03-13 18:57
  • 134

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 (siz...
  • github_34514750
  • github_34514750
  • 2016-08-28 10:10
  • 385

88. Merge Sorted Array [easy] (Python)

题目链接https://leetcode.com/problems/merge-sorted-array/题目原文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted arra...
  • coder_orz
  • coder_orz
  • 2016-06-15 13:54
  • 2577

[C语言][LeetCode][88]Merge Sorted Array

题目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 ha...
  • Timsley
  • Timsley
  • 2015-08-20 08:29
  • 385
    个人资料
    • 访问:41568次
    • 积分:1355
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:19篇
    • 译文:0篇
    • 评论:11条
    最新评论