Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int[] twoSum2(int[] numbers, int target) {
//返回int类型的数组
int[] result = new int[2];
//声明map
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
result[1] = i ;
result[0] = map.get(target - numbers[i]);
return result;
}
map.put(numbers[i], i );
}
return result;
}
}
最后
以上就是耍酷星月最近收集整理的关于Leetcode1的全部内容,更多相关Leetcode1内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复