【Leetcode】Two Sum II #Too easy, just review hashmap
来源:互联网 发布:监控干扰器软件 编辑:程序博客网 时间:2024/04/29 23:12
Given an array of integers that is already sorted in ascending order, 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[] ind = {-1,-1}; //put array into the map for(int i=0;i<numbers.length;i++){ map.put(numbers[i],i); } //look for matching half by ensuring the index is appropriate for(int i=0;i<numbers.length;i++){ if(map.get(target - numbers[i])==null) continue; if(map.get(target - numbers[i]) > i){ ind[0] = i+1; ind[1] = map.get(target - numbers[i])+1; return ind; } } return ind; }}
0 0
- 【Leetcode】Two Sum II #Too easy, just review hashmap
- leetcode 167[easy]---Two Sum II
- Leetcode[Easy] - Two Sum
- 【Leetcode】Two sum(easy)
- leetcode Two Sum easy
- [leetcode] Two Sum II
- LeetCode : Two Sum II
- leetcode [Two Sum II
- 【LeetCode】Two Sum II
- 【leetcode】Two Sum II
- LeetCode:Two Sum II
- leetcode--Two Sum II
- leetcode Two Sum II
- leetcode -- Two Sum II
- leetCode-Two Sum II
- Leetcode #1 Two Sum (easy)
- Leetcode #1 - Two Sum - Easy
- leetcode 1. Two Sum [Easy]
- SpriteFrameCache精灵框使用
- Hibernate入门
- mongodb的读写分离
- jQuery插件jQuery Validation学习(三)——validator对象
- 质数筛选(详细解释以及模板)(Eratosthenes)
- 【Leetcode】Two Sum II #Too easy, just review hashmap
- cocos2d-x 创建精灵的五种方法
- Hdu 2024C语言合法标识符
- 设计模式——工厂方法模式
- Redis 源码分析 (一)Redis竟然用 C 语言进行面向对象编程
- Android Tip : Android 获取当前Activity对象的方式:
- 身份证号码编码方法及校验算法
- 【高斯消元】[SGU275]To xor or not to xor
- ubuntu需要额外安装的包用于支持android sdk的运行