Finding the closest number in a random set

前端 未结 6 1997
长情又很酷
长情又很酷 2021-01-01 05:23

Say I got a set of 10 random numbers between 0 and 100.

An operator gives me also a random number between 0 and 100. Then I got to find the number in the set that is

6条回答
  •  灰色年华
    2021-01-01 05:37

    1. order the set
    2. binary search for the input
    3. if you end up between two elements, check the difference, and return the one with the smallest difference.

提交回复
热议问题