Finding the two closest numbers in a list using sorting

前端 未结 3 1039
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 09:20

If I am given a list of integers/floats, how would I find the two closest numbers using sorting?

3条回答
  •  醉梦人生
    2021-01-27 09:27

    It could be more than one possibilities. Consider this list

    [0,1, 20, 25, 30, 200, 201]
    

    [0,1] and [200, 201] are equal closest.

提交回复
热议问题