import java.util.HashMap;
import java.util.Map;
/**
* @author xienl
* @description 两数之和
* @date 2022/7/7
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public int[] twoSum (int[] numbers, int target) {
// write code here
int[] res = new int[2];
// write code here
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < numbers.length; i++){
if (map.containsKey(numbers[i])){
res[0] = map.get(numbers[i]);
res[1] = i + 1;
} else{
map.put(target - numbers[i], i + 1);
}
}
return res;
}
}
牛客网:NC61 两数之和
最新推荐文章于 2022-11-04 07:00:00 发布