zoukankan      html  css  js  c++  java
  • leetcode 88 Merge 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()
    


     

  • 相关阅读:
    「CSP-S 2019」初赛解析
    「SP741」STEAD
    「CF1382B」Sequential Nim
    「二分图」学习笔记
    题解 P3321 【[SDOI2015]序列统计】
    题解 P5339 【[TJOI2019]唱、跳、rap和篮球】
    题解 P3200 【[HNOI2009]有趣的数列】
    题解 P2606 【[ZJOI2010]排列计数】
    题解 CF1095F 【Make It Connected】
    题解 CF1155E 【Guess the Root】
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/7076289.html
Copyright © 2011-2022 走看看