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

Leetcode练习题Two Sum的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  7. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  8. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  9. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

随机推荐

  1. Appium+java --连接模拟器画面倒过来的问题

    引用文章:https://blog.csdn.net/testerYu/article/details/90024049 工具 夜神模拟器 appium 现象 通过代码运行截图操作,结果全是倒着的如下 ...

  2. P3376 网络最大流模板(Dinic + dfs多路增广优化 + 炸点优化 + 当前弧优化)

    ### P3376 题目链接 ### 这里讲一下三种优化的实现以及正确性. 1.dfs多路增广优化 一般的Dinic算法中是这样的,bfs() 用于标记多条增广路,以至于能一次 bfs() 出多次 d ...

  3. TCP协议 - 面向连接

    一.TCP特性概览 1.面向连接 TCP是基于连接进行数据交互,通信双方在进行数据交互之前需要建立连接,该连接也只能用在双方之间进行交互.这点不像UDP中的组播和广播,可以在同一组中多个主机交互数据. ...

  4. java高并发系列 - 第10天:线程安全和synchronized关键字

    这是并发系列第10篇文章. 什么是线程安全? 当多个线程去访问同一个类(对象或方法)的时候,该类都能表现出正常的行为(与自己预想的结果一致),那我们就可以所这个类是线程安全的. 看一段代码: pack ...

  5. paypal开发指南

    一.开发者地址: https://developer.paypal.com 使用在paypal上注册的账号登陆即可, 二.沙箱账号 paypay自动会为你创建两个沙箱账号,一个商家,一个买家.在acc ...

  6. Dynamics CRM定制子网格添加按钮实例之二:调试代码、打开Web资源及获取选择的记录

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复222或者20160501可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  7. MySQL 部署 MHA 高可用架构 (二)

    实现 MHA VIP 功能 配置 master_ip_failover 脚本(db3) 把 master_ip_failover 上传到 /iba/software 上 master_ip_failo ...

  8. day35作业

    1. 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重) -- 2.查询每个老师教授的课程数量 和 老师信息 -- 3. 查询学生的信息以及学生所在的班级信息 -- 4.学生中男生的个 ...

  9. java实现序列化的两种方式

    1.Serializable接口 2.Externalizable接口 public class Demo2 implements Externalizable{ transient private ...

  10. [PHP] Ubuntu快速安装起PHP7.4

    先安装一下这个命令 add-apt-repositoryapt-get install software-properties-common 添加第三方源:add-apt-repository ppa ...