Funny solution to youdao puzzle 2, time complexity O (1)

Source: Internet
Author: User

Classic lookup Method

Static int [] arr;

Static T2 ()
{
Arr = new int [1593];
Arr [0] = 1729;
Arr [1] = 4104;
Arr [2] = 13832;
Arr [3] = 20683;
Arr [4] = 32832;
Arr [5] = 39312;
Arr [6] = 40033;
Arr [7] = 46683;
.........
Arr [1585] = 1156658048;
Arr [1586] = 1167270776;
Arr [1587] = 1181984832;
Arr [1588] = 1217741768;
Arr [1589] = 1228225789;
Arr [1590] = 1231216119;
Arr [1591] = 1234859661;
Arr [1592] = 1477354411;
}

Public static int count (int n)
{
Int max = 1592;
Int min = 0;
Int cur = 0;
While (max> = min)
{
Cur = (MAX + min)/2;
If (n <arr [cur])
{
Max = cur-1;
}
Else if (n> arr [cur])
{
Min = cur + 1;
}
Else
{
Break;
}
}
If (max <min)
{
Cur = max;
}
Return cur + 1;
}

Internally implement a binary search with an excessively large value returned when a failure occurs.

475574 of 10 queries took 10 ms

It is only for play, but it is actually the internal processing method of many mathematical libraries. I don't believe that math. Sin will perform actual operations.

Full code/files/Chinese-Xu/t2_chinesexutable.rar

 

 

Related Article

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.