C++ 1 for-loop and 3 ?: operators solution in Time O(n) Space O(1)


  • 0
    O
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) 
    {
        for(int i = m + n - 1, m_i = m - 1, n_i =  n - 1; i >= 0; i--)
        {
            nums1[i] = (m_i < 0)? nums2[n_i--]: 
                       (n_i < 0)? nums1[m_i--]: 
                       (nums1[m_i] > nums2[n_i])? nums1[m_i--]:
                                                  nums2[n_i--];
        }
    }

Log in to reply
 

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