1 Two Sum:
Question
Solution
知识点总结
常见方法
HashMap由value获得key
Question:
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, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Solution:
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap hashmap = new HashMap();
for(int i=0;i
知识点总结:
ClassMap
K - the type of keys maintained by this map
V - the type of mapped values
常见方法:
- clear:
Removes all of the mappings from this map.
- containsKey:
Returns true if this map contains a mapping for the specified key.
- containsValue:
Returns true if this map maps one or more keys to the specified value.
- get:
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
- put:
Associates the specified value with the specified key in this map.
- size:
Returns the number of key-value mappings in this map.
- replace:
Replaces the entry for the specified key only if it is currently mapped to some value.
- isEmpty:
Returns true if this map contains no key-value mappings.
- remove:
Removes the mapping for the specified key from this map if present.
- keySet:
Returns a Set view of the keys contained in this map.
HashMap由value获得key:
由于hashmap中key值唯一,而value值不唯一;所以一般都是通过get函数实现,获得value值;而如果想通过value获得key这需要自己写,可通过如下操作;
- 查找一个key值:
public class HashMap
{
public static String getKey(HashMap hashmap,int v alue)
{
int findValue = 0;
//迭代循环
for(Integer getKey:hashmap.keySet())
{
if(hashmap.get(getKey).equals(findValue))
{
findValue = getKey
}
}
return findValue;
}
}
- 查找一个key集合
public static List getKeyList(HashMap hashmap, int value)
{
List list = new ArrayList();
for(Integer getKey:hashmap.keySet())
{
if(hashmap.get(getKey).equals(value))
{
list.add(getKey);
}
}
return list;
}
References:
HashMap API