Created
April 4, 2019 06:23
-
-
Save Ahmah2009/e4e60e596968dfaafafba04ea284f10f to your computer and use it in GitHub Desktop.
973. K Closest Points to Origin leetcode
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
import "sort" | |
func kClosest(points [][]int, K int) [][]int { | |
sort.SliceStable(points,func(i,j int)bool{ | |
point1 := points[i] | |
point2 := points[j] | |
return distance(point1[0],point1[1]) < distance(point2[0],point2[1]) | |
}) | |
return points[:K] | |
} | |
func distance(x, y int) int { | |
return x*x + y*y | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment