01Two Sum
来源:互联网 发布:javascript 函数 编辑:程序博客网 时间:2024/06/15 00:44
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].
考点:map映射用来存放键值对,根据键就能查找到值
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<nums.length;i++){
if (map.get(nums[i]) != null) {
int[] result = {map.get(nums[i]), i};
return result;
}
map.put(target - nums[i], i);
}
int[] result = {};
return result;
}
阅读全文
0 0
- LeetCode 01 Two Sum
- LeetCode 01 Two Sum
- Leetcode 01 Two Sum
- 01Two Sum
- LeetCode:01:Two Sum
- LeetCode OJ 01 Two Sum
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- flex布局详解
- Stylus
- 文件对比工具Beyond Compare使用方法(BCompare)
- ionic+极光推送:Error: Variable(s) missing (use: --variable APP_KEY=value).
- Android编程权威指南(第2版)—第13章挑战练习
- 01Two Sum
- Java IO流学习总结
- 2表联合查询
- XYNUOJ 1106 1107 求最大公约数 求最小公倍数
- 设置logo和背景以及语言包切换
- 二叉树的构建及其遍历算法
- POJ 1182 食物链(并查集 带权路径)
- Redis错误及解决方案
- android studio 创建aidl