Last active
December 18, 2015 12:18
-
-
Save laonger/5781396 to your computer and use it in GitHub Desktop.
一个已排好序的数组,求其中两个元素(a和b),使得(a-b)的平方最小(要求复杂度尽可能低)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def a(l): | |
c, n = l[-1] - l[-2], 0 | |
r = len(l) -2 | |
while n <= len(l) -3: | |
cc = l[n+1] - l[n] | |
if cc < c: | |
r, c = n, cc | |
n = n +1 | |
return r, r+1 | |
print a([1, 4, 6, 23, 53, 56, 74, 254, 324, 345, 9898]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment