leetcode--js--Two Sum
问题描述:
给定一个整数数列,找出其中和为特定值的那两个数。
你可以假设每个输入都只会有一种答案,同样的元素不能被重用。
示例:
给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
我的答案:
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var a=[];
for(var i=0;i<nums.length-1;i++){
for(var j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
a.push(i);
a.push(j);
}
}
}
return a;
};
优秀答案:
参考 http://www.cnblogs.com/kiznaiver1998/p/8605809.html
解题思路:构造了arr{ key:value} 对象。将target-nums[i] 差值放在arr{ }对象中,并存储其位置i。然后每次就在arr{ }对象中找有没有对应的数即可。
function twoSum(nums, target) {
var arr = {};
for (var i = 0; i < nums.length; i++) {
if (typeof(arr[nums[i]]) !== "undefined"){
return [arr[nums[i]], i];
}
arr[target - nums[i]] = i;
}
}
leetcode--js--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 ...
- [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 ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
随机推荐
- JS中字符串切片
1.charAt 作用:根据索引值获取字符串 s1= "Hello world"; // 根据索引求字符 var myChar = s1.charAt(4); console.lo ...
- 一张图快速上手Xmind思维导图
使用软件:Xmind8 pro版(可在网上找破解版),应用广泛,功能强大
- 实验四:划分多个VLAN
1.配置图 2.配置命令 Switch1.Switch2.Switch3的配置是一样的,如下所示:(可直接复制交换机,可以只配置一次) 通过命令查看配置: Switch0的配置如下: 通过命令查看tr ...
- 说实话 NuGet Package Manager 网速不是一般的慢
- 演示共享布局 Demonstrating Shared Layouts 精通ASP-NET-MVC-5-弗瑞曼 Listing 5-10
- Hbase与Maven工程的Spring配置笔记
1.HBase基本操作 hbase shell: 连接到正在运行的HBase实例 help: 显示一些基本的使用信息以及命令示例. 需要注意的是: 表名, 行, 列都必须使用引号括起来 create ...
- HTTP访问控制模块(HTTP Access)
·摘要这个模块提供简单的基于主机的访问控制.ngx_http_access_module这个模块可以详细的检查客户端IP,并且按顺序执行第一条匹配的规则.如下例: location / { deny ...
- Spring注解开发系列Ⅴ --- 自动装配&Profile
自动装配: spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值.自动装配的优点有: 自动装配可以大大地减少属性和构造器参数的指派. 自动装配也可以在解析对象时更新配置. 自动装配的 ...
- .net core控制台使用log4net
第一步,Nuget log4net包 第二步,在项目中添加一个新xml文件,我这里是直接从.net framework的项目里复制过来的config文件,不过效果是一样的 内容如下: ?xml ver ...
- pymysql连接提示format: a number is required, not str
最近想随手写一个简单的员工管理系统,第一次使用python连接数据库,在这个过程中就遇到了一些问题,遂记录 遇到问题习惯性百度一下,很多教程都不适合新手,有些还不知道是不是瞎写的,所以我觉得有必要自己 ...