术语:
Function:一组语句可以一起执行某些操作
背景
完成LeetCode 挑战的其中一件事是完成它们可以帮助您探索所选语言的极限(在本例中,我们谈论的是 Swift)。
现在,在完成这些竞赛时似乎重新出现的挑战之一是使用二分搜索(一个例子是挑战1552),它要求程序员开发一个二分搜索,但使用自定义函数来决定我们是否查看中点右侧的左侧。
class Solution {
func maxDistance(_ position: [Int], _ m: Int) -> Int {
let position = position.sorted()
let length = position.count
func ballsWillFit(_ d: Int) -> Int {
var result = 1
var cur = position[0]
for pos in position where (pos - cur) >= d {
cur = pos
result += 1
}
return result
}