leetcode 88 Merge Sorted Array

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

 

 

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

Note:
You may assume that nums1 has enough space (size that is greater or equal tom + n) to hold additional elements from nums2. The number of elements initialized innums1 and nums2 are m and n respectively.

 

测试用例:

 

Runtime Error Message:
Last executed input:
Input: [1,2,3,0,0,0], 3, [2,5,6], 3
Output: [1,2,3,5,6]
Expected: [1,2,2,3,5,6]

 

错误的解决方案:

 

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 
    {
    set<int> result;
    for(int i = 0;i<m;i++)
    {
        result.insert(nums1[i]);
    }
    for(int i = 0;i<n;i++)
    {
        result.insert(nums2[i]);
    }
    nums1.clear();
    set<int>::iterator iter = result.begin();
    for(;iter!=result.end();iter++)
    {
        nums1.push_back(*iter);
    }
    }
};


 

我的解决方案:上面就是相同 的元素没装进来,换成multiset就行了

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 
    {
    multiset<int> result;
    for(int i = 0;i<m;i++)
    {
        result.insert(nums1[i]);
    }
    for(int i = 0;i<n;i++)
    {
        result.insert(nums2[i]);
    }
    nums1.clear();
    set<int>::iterator iter = result.begin();
    for(;iter!=result.end();iter++)
    {
        nums1.push_back(*iter);
    }
    }
};


 简短的解决方案:

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        int k = m + n;
        while (k-- > 0)
            A[k] = (n == 0 || (m > 0 && A[m-1] > B[n-1])) ?  A[--m] : B[--n];
    }
};


可读性较好:

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


python解决方案:

class Solution:
# @param A  a list of integers
# @param m  an integer, length of A
# @param B  a list of integers
# @param n  an integer, length of B
# @return nothing(void)
def merge(self, A, m, B, n):
    x=A[0:m]
    y=B[0:n]
    x.extend(y)
    x.sort()
    A[0:m+n]=x


python解决方案2:thats why we love python

def merge(self, A, m, B, n):
        A[m:] = B[:n]
        A.sort()


 

 

相关文章
|
11月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
35 0
|
11月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
44 0
Search in Rotated Sorted Array - 循环有序数组查找问题
Search in Rotated Sorted Array - 循环有序数组查找问题
68 0
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
84 0
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口