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. ES5提取公共变量

    在ES5中,请求地址前缀等需要作为公共变量提出. 则需在common.js写上 Object.defineProperty(window,'base',{ value:"http://xx. ...

  2. 图解Java常用数据结构

    最近在整理数据结构方面的知识, 系统化看了下 Java 中常用数据结构, 突发奇想用动画来绘制数据流转过程. 主要基于 jdk8, 可能会有些特性与 jdk7 之前不相同, 例如 LinkedList ...

  3. 全网趣味网站分享:今日热榜/Pixiv高级搜索/win10激活工具/songtaste复活/sharesome汤不热替代者

    1.回形针手册 由科普类视频节目“回形针PaperClip”近期提出的一个实用百科工具计划,计划名称是回形针手册. 包含了当下科技,农业等等各行各业的各种相关信息,计划刚刚开始! 关于回形针手册的详细 ...

  4. java在hashmap初始化时赋初值

    Java中的HashMap是一种常用的数据结构,一般用来做数据字典或者Hash查找的容器. 一般我们初始化并赋初值是这样做的: HashMap<String, Object> map = ...

  5. Django多进程滚动日志的问题

    使用RotatingFileHandler控制日志文件的大小 # settings.py LOGGING = { ... 'handlers': { ... 'file': { 'level': 'I ...

  6. 【LOJ#6682】梦中的数论(min_25筛)

    [LOJ#6682]梦中的数论(min_25筛) 题面 LOJ 题解 注意题意是\(j|i\)并且\((j+k)|i\), 不难发现\(j\)和\((j+k)\)可以任意取\(i\)的任意因数,且\( ...

  7. 数据库——数据库设计 E-R图向关系模型的转换

    1.将下列物资管理E-R图转换为关系模式: 转换原则 ⒈  一个实体型转换为一个关系模式.关系的属性:实体型的属性关系的码:实体型的码   ⒉ 一个m:n联系转换为一个关系模式(初步,以后可能调整). ...

  8. 公益:开放一台Nacos服务端给各位Spring Cloud爱好者

    之前开放过一台公益Eureka Server给大家,以方便大家在阅读我博客中教程时候做实验.由于目前在连载Spring Cloud Alibaba,所以对应的也部署了一台Nacos,并且也开放出来,给 ...

  9. 无法打开锁文件 /var/lib/dpkg/lock-frontend - open

    转自:https://blog.csdn.net/sinat_29957455/article/details/89036005 在使用apt-get安装程序的时候报: E: 无法打开锁文件 /var ...

  10. Python 最强 IDE 详细使用指南!-PyCharm

    PyCharm 是一种 Python IDE,可以帮助程序员节约时间,提高生产效率.那么具体如何使用呢?本文从 PyCharm 安装到插件.外部工具.专业版功能等进行了一一介绍,希望能够帮助到大家. ...