leetcode-two sum
来源:互联网 发布:58淘宝模特兼职的骗局 编辑:程序博客网 时间:2024/04/30 11:27
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
public class Solution { public int[] twoSum(int[] numbers, int target) { HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); int len =numbers.length; if(len<=1)return null; for(int i=0;i<len;i++){ int dif=target-numbers[i]; if(map.containsKey(dif)){ int[] result={map.get(dif)+1,i+1}; return result; } map.put(numbers[i],i); } return null; }}1 the same key value can only have once.
2 still the same idea with three or four keys. make one set, and try the other one.
3 The most wonderful character about hash table is that it can remember past keys and the value associated with it. And can efficiently search!
0 0
- 【Leetcode】Two Sum (Sum)
- LeetCode: Two Sum
- LeetCode: Two Sum
- [Leetcode] Two Sum
- LeetCode : Two Sum
- Leetcode: Two Sum
- leetcode 46: Two Sum
- [LeetCode] Two Sum
- LeetCode 1 - Two Sum
- Leetcode : Two Sum
- 【leetcode】Two Sum
- [LeetCode]Two Sum
- leetcode Two Sum
- [Leetcode] Two Sum
- [leetcode] Two Sum
- LeetCode - Two Sum
- leetcode之Two Sum
- LeetCode | Two Sum
- UI基础第五弹:懒加载
- 学习内核Kconfig和Makefile原理 .
- UI基础第六弹:九宫格坐标计算
- Linux kernel的Makefile和Kconfig以及Make menuconfig的关系 .
- UI基础第七弹:字典转模型
- leetcode-two sum
- UI基础第八弹:xib的简单使用
- UI基础第九弹:从代码的逐步优化看MVC
- UI基础第十弹:Kvc简单介绍
- 看天下势:论微商的风险
- UI基础第十弹:KOC简单介绍
- 通过xml生成word文档
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 《设计模式之禅》笔记17 -- 观察者模式