HDU可能是人太多,服务器撑不住总是挂,故转战 LeetCode。
LeetCode 可以看到测试数据,方便debug,这一点真是太赞了。
这道题我提交了13次,花了。。。3 个小时?哈哈,醉了,弱爆了。看来以后有空可以多刷刷题。
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
1 | Given nums = [2, 7, 11, 15], target = 9, |
有且只有两个数的和 = target
那么树的深度最多2层,虽然是暴力搜索,这道题不暴力还真不行,这么小的深度再加上剪枝肯定没问题。
花了那么久,到后面才想起来用一个flag来做剪枝。
LeetCodeAC code
1 | class Solution { |