1 Two Sum:

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<Integer,Integer> hashmap = new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++)
{
hashmap.put(nums[i],i);
} int v1=0,v2=0;
for(int m=0;m<nums.length;m++)
{
int complement = target-nums[m];
if (hashmap.containsKey(complement)&&( hashmap.get(complement)!=m))
{
return new int[] {m,hashmap.get(target-nums[m])};
}
}
throw new IllegalArgumentException("No two sum solution"); }
}

知识点总结:

ClassMap<K,V>

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<Integer,Integer> 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<Integer> getKeyList(HashMap<Integer,Ingeger> hashmap, int value)
{
List<Integer> list = new ArrayList();
for(Integer getKey:hashmap.keySet())
{
if(hashmap.get(getKey).equals(value))
{
list.add(getKey);
}
}
return list;
}

References:

HashMap API

最新文章

  1. Python列表去重
  2. 将声音文件加入VC
  3. 开发https应用
  4. [收藏]ASP.NET MVC管道详述
  5. 为SpringMvc项目安装BootStrap和AngularJs前端框架
  6. MVC中用ajax提交json对象数组
  7. c语言的结构体字节数统计
  8. C#获取程序集的版本号和最后编译时间
  9. spring mvc处理流程概述
  10. Android开发----权限大全
  11. VB编程技巧推荐
  12. linux应用程序地址布局
  13. NLP句子表征,NLP 的巨人肩膀(下):从 CoVe 到 BERT (转载)
  14. turtle库的学习
  15. 简单gitblit与Jenkins结合,持续集成
  16. openpyxl一点心得
  17. FastDFS客户端与自定义文件存储系统
  18. Mac 上卸载 Java
  19. NDArray自动求导
  20. 重读《深入理解Java虚拟机》六、Java泛型 VS C#泛型 (伪泛型 VS 真泛型)

热门文章

  1. H5混合应用之上下文切换
  2. 解决 canvas 下载含图片的画布时的报错
  3. Http Header的Transfer-Encoding
  4. .NET 使用OLEDB导入Excel数据
  5. Web前端—— JQuery迷你版实现以及使用
  6. MySQL 示例数据库
  7. Docker配置yapi接口
  8. PHP+Ajax点击加载更多列表数据实例
  9. int[]里数的个数怎么由输入决定?-----动态数组。
  10. 使用Wireshark进行DNS协议解析