思考练习

来源:互联网 发布:淘宝网兼职 编辑:程序博客网 时间:2024/06/06 01:35

Question:
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:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution:
1:双重循环,获取所有元素,两两组合,判断运算是否成立,获取下标值(笨办法,效率低)
2:利用HashMap