[Leetcode]-004-median of Sorted Arrays

Source: Internet
Author: User

Website: https://leetcode.com/problems/median-of-two-sorted-arrays/

Test instructions

Gave two sets of ordered numbers to find out their total median

Tips:

The median is the value of the equal number of left and right, and if it does not exist, the average of the two numbers for the number of equal numbers.

Solution 1:

Combine two sets of numbers into a group of numbers. It can also be O (m+n).

But the spatial complexity is O (m+n).

There is obviously a better solution.

Solution 2:

Because the two groups of numbers are already in order, it is assumed to be a group of numbers, with only two subscripts.

Each time by the subscript, the comparison of the two groups of the subscript represented by the value of the order, find the median, the end.

Note that you need to record two values, because the median may be the average of two numbers.

In this way, the spatial complexity is O (1), and the time complexity should be less than O (m+n).

Code:

Https://github.com/LiLane/leetcode/blob/master/java/004-MedianofTwoSortedArrays-201504151322.java

Https://github.com/LiLane/leetcode/blob/master/c%2B%2B/004-MedianofTwoSortedArrays-201504151310.cpp


[Leetcode]-004-median of Sorted Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.