LeetCode 1. Two Sum (JavaScript)
1. Two Sum
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].
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let map=new Map();
for(let i=0;i<nums.length;i++){
let index=map.get(target-nums[i]);
if(index!=undefined){
return [index,i];
}
map.set(nums[i],i);
}
};
LeetCode 1. Two Sum (JavaScript)的更多相关文章
- 「LeetCode」0001-Two Sum(Ruby)
题意与分析 题意直接给出来了:给定一个数,返回数组中和为该数(下为\(x\))的两个数的下标. 这里有一个显然的\(O(n)\)的实现:建立一个hash表,每次读入数(记作\(p\))的时候查询has ...
- leetcode 1 Two Sum(查找)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- LeetCode 1 Two Sum(二分法)
题目来源:https://leetcode.com/problems/two-sum/ Given an array of integers, find two numbers such that t ...
- [Leetcode] 1.Two Sum(unordered_map)
1.首先想到的方法就是两个for循环全部遍历,代码如下,可通过,但效率太低 class Solution { public: vector<int> twoSum(vector<in ...
- Leetcode部分题目整理(Javascript)
3.无重复字符的最长子串 /** * @param {string} s * @return {number} */ var lengthOfLongestSubstring = function(s ...
- leetcode 之 two sum (easy)c++
1.数组的长度 length() .容器vector长度 size() .容器vector vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库. ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)
Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...
- 深入理解Delete(JavaScript)
深入理解Delete(JavaScript) Delete 众所周知是删除对象中的属性. 但如果不深入了解delete的真正使用在项目中会出现非常严重的问题 (: Following 是翻译 ka ...
随机推荐
- 【杂题】[LibreOJ 2541] 【PKUWC2018】猎人杀【生成函数】【概率与期望】
Description 猎人杀是一款风靡一时的游戏"狼人杀"的民间版本,他的规则是这样的: 一开始有 n个猎人,第 i 个猎人有仇恨度 wi.每个猎人只有一个固定的技能:死亡后必须 ...
- webpack原理探究 && 打包优化
在做vue项目和react项目时,都用到了webpack.webpack帮助我们很好地提高了工作效率,但是一直以来没有对其原理进行探究,略有遗憾. 因为使用一个工具,能够深入了解其原理才能更好地使用. ...
- Hive 外部表新增字段或者修改字段类型等不生效
标题比较笼统,实际情况是: 对于Hive 的分区外部表的已有分区,在对表新增或者修改字段后,相关分区不生效. 原因是:表元数据虽然修改成功,但是分区也会对应列的元数据,这个地方不会随表的元数据修改而修 ...
- Java的枚举类型
引用并转载于:http://blog.csdn.net/ishallwin/article/details/9440251 1.什么是枚举: 在实际编程中,往往存在着这样的“数据集”,它们的数值在程序 ...
- java中的this和super(构造函数)
1.this:表示当前对象 常用的代码: public class A{ private String name; public void setName(String name){ this.nam ...
- 我的“MIT Challenge”
前言 在学习之余看到了一个有趣的挑战,名叫"MIT Challenge",这个挑战的目标是在一年365天之内学习 MIT 计算机系本科本科学生四年的课程.自己大二学习算法时也曾学习 ...
- HTML5--(2)属性选择器+结构性伪类+伪类
一.属性选择器 [att] 匹配所有具有att属性的 [att=val] 匹配所有att属性等于“val”的 [att~=val] 匹配所有att属性包含“val”或者等于“val”的(val必须是一 ...
- Vue 多路由文件的合并
Vue 多路由文件的合并 1.使用的是ES6 数组的合并方法 let routes = new Set([...routes1, ...homerouters]);2.两个路由文件,导出的实际上就是一 ...
- 一键安装lamp环境出现的问题
前言:之前安装lamp是独立安装的,安装扩展很方便,现在用这个一键安装包,不知道怎么样,尝试一把. Part1:安装过程中出现的问题 error: utf8_mime2text() has new s ...
- Linux下一个最简单的不依赖第三库的的C程序(2)
一个最简单的C程序,如下: main.c: int main() { char *str = "Hello World"; ; } 在64位平台上编译一个32位的程序,如下:(32 ...