[JS做LeetCode] Two Sum

Two Sum

<html>

<head>
    <meta http-equiv="charset" content="utf-8"></head>

<body>
    <script>
        var numbers=[2, 7, 11, 15];
        var target = 9;

        var twoSum = function(numbers, target) {
                // 用对象来模拟hash table
                // 将所有数组放进hash里面,key为当前值,value为下标
                var hash = {};
                for(var i=0; i<numbers.length; i++) {
                        hash[numbers[i]] = i;
                }

                // 遍历数组,如果hash里面有key和数组当前的值相加为target,那么就是那两下标了
                // 注意要判断两下标不能一样
                for(var i=0; i<numbers.length; i++) {
                        var needValue = target-numbers[i];
                        if(hash.hasOwnProperty(needValue)) {
                                var index1 = i+1;
                                var index2 = hash[needValue]+1;
                                if(index1!==index2) {
                                        return [index1, index2];
                                }
                        }
                }
        };

        alert(twoSum(numbers, target));
    </script></body>
时间: 2024-08-03 16:13:30

[JS做LeetCode] Two Sum的相关文章

[JS做LeetCode] Median of Two Sorted Arrays

Median of Two Sorted Arrays <html> <head> <meta http-equiv="charset" content="utf-8"></head> <body> <script> //解题思路: //找出中位数要循环的次数为loop //posA为数组A的下标,posB为数组B的下标 //每次循环找出两个数组对应下标比较小的值,进栈,下标+1 //注意判断数

[JS做LeetCode] Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters <html> <head> <meta http-equiv="charset" content="utf-8"></head> <body> <script> // 解题思路: // 设置两个指针,start和end,end一直向后走,如果遇到前面出现过的字符串,start向前走一步 var s =

LeetCode: Path Sum [112]

1. 拿到一个新 bug, 首先要重现问题. 这对 code 问题是必须的, 对客户的 data 问题, 几乎也是必须的. 如果是 code 问题, 不重现就没办法修改代码, 改好了也无法验证是不是改对了. 客户的 data 出问题, 多数情况也是能够重现的. 毕竟客户是用我们的系统操作的, 只要拿到客户的历史数据, 对照着是可以自己做出同样的数据. 以前我遇到 data fix 的时候不喜欢重现, 都是凭感觉给出脚本. 但这样常常忽略一些重要的数据, 容易出错. 如果确定是 data fix,

[LeetCode] Two Sum [17]

题目 Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note th

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

leetcode -- 3 sum

3-sum 题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 题目要求: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ 

用js做登录的用户名判定

<script type="text/javascript" language="javascript" src="JS/jquery.js"></script>    <script language="javascript" type="text/javascript">      $(document).ready(function() {            

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

[3] 用D3.js做一个简单的图表吧!

本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 前面说了几节,都是对文字进行处理,这一节中将用 D3.js 做一个简单的柱形图. 做柱形图有很多种方法,比如用 HTML 的 div 标签,或用 svg . 推荐用 SVG 来做各种图形.SVG 意为可缩放矢量图形(Scalable Vector Graphics),SVG 使用 XML 格式定义图像,不清楚什么是SVG的朋友请先在 w3cschools 学习下