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 ...
随机推荐
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 3
23.2 接口实现的基础 大家都很了解函数在本地应用,通过名称调用函数执行,并通过传递不同参数,函数有不同执行,执行后给调用者返回结果.如果把一个函数做成一个接口远程访问,也需要这几个步骤.使用HT ...
- 洛谷 P1351 (枚举)
### 洛谷P1351 题目链接 ### 题目大意: 给你 n 个节点, n-1 条边的无向联通图.若定义(u,v)表示 u 与 v 点的最短距离,如果 (u,v)值为 2 ,则这两个点的点权之积(即 ...
- Django日志记录详细的报错信息
当服务器500错误的时候,普通日志只会记录一行500的request信息,并不会记录详细的报错定位 [ERROR] 2019-06-12 15:07:03,597 "GET /api/v1/ ...
- Docker - 快速入门(一)
概念 下面这三个概念一开始可能不好理解,等大家跟着博客把例子做完了,再回头来看应该就能理解了. docker image # docker镜像 镜像就是一个只读的模板.镜像可以用来创建Docker容 ...
- python基础(20):序列化、json模块、pickle模块
1. 序列化 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 1.1 为什么要有序列化 为什么要把其他数据类型转换成字符串?因为能够在网络上传输的只能是bytes,而能够 ...
- String与StringBuilder性能比对
//String与StringBuilder性能比对package seday01;/** * String修改字符串带来的性能开销 * @author xingsir * */public clas ...
- Web基础--JavaScript入门
一.JavaScript 1.什么是JavaScript(JS) (1)JavaScript是嵌入HTML中的代码,在浏览器中执行的脚本语言,具有与Java和C语言类似的语法.(2)一种网页编程技术, ...
- KB奇遇记(3):糟糕的IT现状
2015年8月3号,终于告别了过去来到了KB. 公司给安排的住房是一间套房里的小房间,小的简直连坐的地方都没有了,中间一个大床将房间隔了两边,显得特别狭小.由于是刚来,我也不好要求太多.但就这个小房间 ...
- sftp-server 搭建编译
下载开源代码 https://github.com/zwx230741/openssh-portable 编译 # autoconf # ./configure --prefix=xxx # make ...
- PHP代码篇(二)-- array_column函数将二维数组格式化成固定格式的一维数组,及优化查询方法
小白因为经常用到多表查询,比如获取一个会员领取的卡卷list,里面当然包含了1“会员优惠券记录表t_coupon_members”主表,然后2“门店优惠券表t_coupon”,和3“门店信息表t_sh ...