Leetcode--1. Two Sum(easy)
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 sameelement twice.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> mp=new HashMap<Integer,Integer>();
int len=nums.length;
for(int i=;i<len;i++){
if(mp.containsKey(nums[i])){
int value=mp.get(nums[i]);
return new int[]{value,i};
}
else{
mp.put(target-nums[i],i);
}
}
return new int[];
}
}
Leetcode--1. Two Sum(easy)的更多相关文章
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- LeetCode算法题-Sum of Left Leaves(Java实现)
这是悦乐书的第217次更新,第230篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第85题(顺位题号是404).找到给定二叉树中所有左叶的总和.例如: 二叉树中有两个左叶 ...
- LeetCode算法题-Sum of Two Integers(Java实现)
这是悦乐书的第210次更新,第222篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第78题(顺位题号是371).计算两个整数a和b的总和,但不允许使用运算符+和 - .例 ...
- LeetCode--Array--Two sum (Easy)
1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...
- 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算法积 ...
随机推荐
- JAVA虚拟机是?为什么称作是“平台无关的语言”?
Java虚拟机(Java Virtual Machine)简称JVM ,它是抽象化的计算机,有自己完善的硬体架构,如处理器.堆栈.寄存器等,还具有相应的指令系统.JVM屏蔽了与具体操作系统平台相关的信 ...
- Oracle_高级功能(3) synonym和database link
一.同义词synonymconnect sys/123 as sysdba;select * from emp;ORA-00942: 表或视图不存在create synonym emp for sco ...
- git 远程仓库与本地项目关联
在git 中创建一个项目或仓库如起名blog,生成README.md文件,在本地创建一个项目名为blog ,blog里面是代码,此时执行 git remote add origin <ssh协 ...
- POJ1236或洛谷2746或洛谷2812 Network of Schools
POJ原题链接 洛谷2746原题链接 洛谷2812(加强版)原题链接 显然在强连通分量里的所有学校都能通过网络得到软件,所以我们可以用\(tarjan\)求出强连通分量并缩点,统计缩点后每个点的入度和 ...
- 【资料收集】AutomationGuru
http://www.testautomationguru.com/category/docker/
- java 23种设计模式学习。
一.3大类设计模式:创建型,结构型,行为型. a.5种创建型模式:工厂方法,抽象工厂,单例,建造者,原型. b.7种结构型模式:适配器,装饰器,代理,外观,桥接,组合,享元. c.11种行为型模式:策 ...
- YII配置mysql读写分离
Mysql 读写分离 YIi 配置 <?php return [ 'class' => 'yii\db\Connection', 'masterConfig' => [ // 'ds ...
- 含有选择器的 bootstrap菜单
var menu = new BootstrapMenu('#jsmind_container jmnode:not(.root)', { actions: [{ name: '展开节点', onCl ...
- maven mirror , profile , snapshot 和release
1. settings.xml 配置的mirror <mirrors> <mirror> <id>Nexus</id> <name>nexu ...
- java 泛型: 通配符? 和 指定类型 T
1. T通常用于类后面和 方法修饰符(返回值前面)后面 ,所以在使用之前必须确定类型,即新建实例时要制定具体类型, 而?通配符通常用于变量 ,在使用时给定即可 ? extends A : 通配符上 ...