-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Two Sum 不需要多构造出一个 hash 变量啊 #1
Comments
如果用 $0 来做类型推倒的话,还可以再简洁点。 |
哈哈,我猜 不过用上面的代码会导致重复 >< 比如 hash 当然不是必须用,不过用 hash 是为了让效率提升的。只是用 array 的查找,每一次循环没法利用到前面的结果。 |
那个是笔误,var array:[[Int]] = [], 结果应该是 [[1,0],[0,1]]。 |
如果是 另外还有点小问题,比如这个 case : 不过放在 leetcode 上测的时候,第 15 个 case 会超时,可以去 https://leetcode.com/problems/two-sum/ 试一下。 |
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var array = Int
nums.enumerated().forEach { (offset: Int, element: Int)in
if let index = nums.index(where: { (target - element) == $0}) {
array.append(contentsOf: [index,offset])
}
}
return array
}
The text was updated successfully, but these errors were encountered: