Leetcode练习题Two Sum
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:
Leetcode练习题Two Sum的更多相关文章
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- jsp模板
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...
- 机器学习常见的几种评价指标:精确率(Precision)、召回率(Recall)、F值(F-measure)、ROC曲线、AUC、准确率(Accuracy)
原文链接:https://blog.csdn.net/weixin_42518879/article/details/83959319 主要内容:机器学习中常见的几种评价指标,它们各自的含义和计算(注 ...
- webpack资源处理
使用上篇已谈过,这篇纯代码!!~~ <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- Java生鲜电商平台-电商会员体系系统的架构设计与源码解析
Java生鲜电商平台-电商会员体系系统的架构设计与源码解析 说明:Java生鲜电商平台中会员体系作为电商平台的基础设施,重要性不容忽视.我去年整理过生鲜电商中的会员系统,但是比较粗,现在做一个最好的整 ...
- springCloud实战篇——纯文本
什么是微服务架构? 微服务是系统架构的一种设计风格,主旨是将原本独立的系统产分为多个小型的服务,这些服务都在各自的进程中运行.服务与服务之间基于HTTP的RESTful API进行通信协作. 构造背景 ...
- JS基本语法---while循环
循环:一件事不停的或者是重复的做 循环要有结束的条件,循环还应该有计数器(记录循环的次数的) while循环 while循环语法: 计数器 var 变量=0; while(循环的条件){ ...
- Web基础--HTML、Css入门
一.Web项目(可跳过,直接从下一个标题开始) 1.Web项目: 指的是带网页的项目,通过浏览器可以访问的项目.比如:淘宝.天猫等. 2.Web项目构成: 浏览器(客户端).服务器.数据库. 3.Ja ...
- 熟悉的味道——从Java单例写到C++单例
设计模式中,单例模式是常见的一种.单例模式需要满足以下两个条件: 保证一个类只能创建一个示例: 提供对该实例的全局访问点. 关于单例最经典的问题就是DCL(Double-Checked Lock),今 ...
- wamp环境下composer及laravel的安装配置
laravel: PHP Web开发框架 composer: PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 一.composer安装 参考:Windows ...
- 为什么 Redis 为什么如此受欢迎
现在大多数开发人员都会听说过 Redis.Redis 是目前市场上最好的开源内存 NoSQL 数据库之一.它为前端以及后端服务(如键值查找,队列,哈希等)提供了非常多的帮助. 一.什么是 Redis? ...