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 两数之和
最新推荐文章于 2025-05-15 20:34:11 发布
博客围绕Java和LeetCode算法展开,虽未给出具体内容,但可推测会涉及使用Java语言解决LeetCode上的算法问题,涵盖算法思路、代码实现等信息技术相关内容。

693

被折叠的 条评论
为什么被折叠?



