Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1d1473f

Browse filesBrowse files
authored
add scala solution for K-Closest-Points-To-Origin.scala
1 parent f227ed6 commit 1d1473f
Copy full SHA for 1d1473f

File tree

Expand file treeCollapse file tree

1 file changed

+9
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+9
-0
lines changed
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Solution {
2+
3+
def kClosest(points: Array[Array[Int]], K: Int): Array[Array[Int]] =
4+
points.toList.sortWith(sortBySqrt).take(K).toArray
5+
6+
private def sortBySqrt(arr1 :Array[Int], arr2:Array[Int]): Boolean =
7+
(arr1(0) * arr1(0) + arr1(1) * arr1(1)) < (arr2(0) * arr2(0) + arr2(1) * arr2(1))
8+
9+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.