my 6ms C++ solution


  • 0
    A
    class Solution {
    public:
        void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
            int right = m + n -1;
            m=m-1,n=n-1;
            while(m>=0&&n>=0){
                if(nums1[m]>nums2[n]){
                    nums1[right]=nums1[m];
                    m-=1;
                }else{
                    nums1[right]=nums2[n];
                    n-=1;
                }
                right-=1;
            }
            if(m<0){
                for(int i=0;i<=n;++i){
                    nums1[i]=nums2[i];
                }
            }
        }
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.