码迷,mamicode.com
首页 > 其他好文 > 详细

Two Sum

时间:2014-05-01 08:35:40      阅读:443      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   tar   javascript   ext   strong   get   

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

 

Naive Approach:

mamicode.com,码迷
 1  public int[] twoSum(int[] numbers, int target) {
 2         //Note this is the naive apporach
 3         //the time complexity is n^2.
 4         int[] result = new int[2];
 5         for (int i = 0; i < numbers.length; i++) {
 6             for (int j = i + 1; j < numbers.length; j++) {
 7                 if (target == numbers[i] + numbers[j]) {
 8                     //Note
 9                     result[0] = i + 1;
10                     result[1] = j + 1;
11                     return result;
12                 }
13             }
14         }
15         return result;
16     }
mamicode.com,码迷

 

Using Hash-Map

mamicode.com,码迷
 1  public int[] twoSum(int[] numbers, int target) {
 2         int[] result = new int[2];
 3         HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
 4         for(int i= 0;i<numbers.length;i++){
 5             if(map.containsKey(numbers[i])){
 6                 result[0]=map.get(numbers[i]);
 7                 result[1]=i+1;
 8             }
 9             else {
10                 map.put((target-numbers[i]),i+1);
11             }
12         }
13         return result;
14     }
mamicode.com,码迷

 

 

Basic method of HashMap

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Modifier and Type Method and Description
void clear()
Removes all of the mappings from this map.
Object clone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
boolean isEmpty()
Returns true if this map contains no key-value mappings.
Set<K> keySet()
Returns a Set view of the keys contained in this map.
V put(K key, V value)
Associates the specified value with the specified key in this map.
void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.
V remove(Object key)
Removes the mapping for the specified key from this map if present.
int size()
Returns the number of key-value mappings in this map.
Collection<V> values()
Returns a Collection view of the values contained in this map.

Two Sum,码迷,mamicode.com

Two Sum

标签:des   style   blog   class   code   java   tar   javascript   ext   strong   get   

原文地址:http://www.cnblogs.com/Altaszzz/p/3702504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!