1.Two Sum
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.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
solution:(已A,但这是参考别人的)
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
result[1] = i;
result[0] = map.get(target - numbers[i]);
return result;
}
map.put(numbers[i], i);
}
return result;
}
}
注意:void clear() 从此映射中移除所有映射关系(可选操作)。
boolean containsKey(Object key) 如果此映射包含指定键的映射关系,则返回 true。
boolean containsValue(Object value) 如果此映射为指定值映射一个或多个键,则返回 true。
Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 set 视图。
boolean equals(Object o) 比较指定的对象与此映射是否相等。
V get(Object key) 返回此映射中映射到指定键的值。
int hashCode() 返回此映射的哈希码值。
boolean isEmpty() 如果此映射未包含键-值映射关系,则返回 true。
Set<K> keySet() 返回此映射中包含的键的 set 视图。
V put(K key, V value) 将指定的值与此映射中的指定键相关联(可选操作)。
void putAll(Map<? extends K,? extends V> t) 从指定映射中将所有映射关系复制到此映射中(可选操作)。
V remove(Object key) 如果存在此键的映射关系,则将其从映射中移除(可选操作)。
int size() 返回此映射中的键-值映射关系数。
Collection<V> values() 返回此映射中包含的值的 collection 视图。