Is this cheated?


  • -4
    S

    I'm not sure if this can be the right answer which I use std list.

    class Solution {
    public:
        double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
            list<int>l1(nums1.begin(), nums1.end());
            list<int>l2(nums2.begin(), nums2.end());
            l1.merge(l2);
            int total = nums1.size() + nums2.size();
            int mid = total / 2 + 1;
            l1.resize(mid);
            if (l1.empty()) {
                return (double) 0;
            }
            if (total%2 == 0) {
                double n1 = (double)l1.back();
                if (l1.empty()) {
                    return n1/2;
                } else {
                    l1.pop_back();
                    return (n1 + l1.back()) / 2;
                }
            } else {
                return (double)l1.back();
            }
        }
    };

Log in to reply
 

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