LeetCode OJ 1. Two Sum
2016-01-19 09:30
351 查看
题目:
https://leetcode.com/problems/two-sum/
用Map接口:
https://leetcode.com/problems/two-sum/
题目大意
已知一个数组的所有数值和一个目标值,找到数组中的两个数的索引使得这两个数的和恰好等于目标值。用Map接口:
class Solution { public int[] twoSum(int[] nums, int target) { int[] ans = new int[2]; HashMap<Integer,Integer> hmp = new HashMap<Integer,Integer>(); for(int i = 0;i < nums.length;i++){ Integer temp = hmp.get(nums[i]); if(temp == null) hmp.put(nums[i], i); temp = hmp.get(target - nums[i]); if(temp != null && temp < i){ ans[0] = temp + 1; ans[1] = i + 1; break; } } return ans; } }
相关文章推荐
- ajax多个数值传递
- bzoj1066: [SCOI2007]蜥蜴
- //普通字符串转JSON字符串转字典
- CocoaPods的安装使用和常见问题
- jstl标签
- RFID数据传输常用编码格式
- 笛卡尔积算法的Java实现
- php性能优化
- POJ-魔兽世界之一:备战
- ROM/RAM/DRAM/SRAM/FLASH区别联系总结
- MongoDB的本地化排序
- Android WebView 与登录状态保持一致,建立SESSION会话
- 【HDU】 1010 Tempter of the Bone
- Linux学习27_每天一个linux命令(28):tar命令
- Tomcat之项目部署
- svn删除用户
- docker镜像和容器
- LeetCode Add Binary
- JPA Hibernate应用实例
- FireMonkey的窗口Handle转为Windows窗口的Handle