1. 两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

实现1:

    /**
     * @method twoSum
     * @param {number[]} nums 数组
     * @param {number} target 数字
     * @return {number[]} indexes 下标
     */
    function twoSum(nums, target) {
      for (let i = 0; i < nums.length; i++) {
        const findNum = target - nums[i];
        const findIndex = nums.indexOf(findNum);
        if(findIndex > -1 && findIndex !== i) {
          return [i, findIndex];
        }
      }
    };

实现2: for循环 嵌套 for循环

面试题(6)之 leetcode-001的更多相关文章

  1. [leetCode][001] Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  2. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...

  3. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  4. Leetcode 001. 两数之和(扩展)

    1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...

  5. leetcode 001

    1 Two Sum Difficulty: Easy The Link: https://leetcode.com/problems/two-sum/description/ Description ...

  6. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  7. two Sum ---- LeetCode 001

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. leetcode 001 Two Sun

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  9. C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介

    目录 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + ...

  10. [转]T-SQL_面试题

    [转]T-SQL_面试题 2015-05-19 1 创建表插入数据 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,s ...

随机推荐

  1. 嵊州普及Day5T4

    题意:两个1,每次可将一个*k,一个*K2,n个问题,问能否达成x,y? 思路:只有将x,y相乘为3次方时,才可能.并且相乘的三次方一定要是x,y的因子. 下面证明:3次方易证,因为对每个k,都会乘三 ...

  2. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  3. 容器STL

    一.迭代器iterator 迭代器是容器的一种遍历方式,每种容器都定义了自己的迭代器类型 声明一个迭代器: 容器名称<数据类型>::iterator 迭代器名称 vector<int ...

  4. Java笔记--常用类

    1.String类: --使用Unicode字符编码,一个字符占两个字节: --String类是一个final类,代表不可变的字符序列: --字符串是不可变的,一个字符串对象一旦被配置,其内容是不可变 ...

  5. ACM-小偷的背包

    题目描述:小偷的背包   设有一个背包可以放入的物品重量为S,现有n件物品,重量分别是w1,w2,w3,...,wn.问能否从这n件物品中选择若干件放入背包中,使得放入的重量之和正好为S.如果有满足条 ...

  6. 转载-- SQL连接查询2 外连接(左右联接查询)

    http://www.cnblogs.com/zhangqs008/archive/2010/07/02/2341196.html 外连接主要包括左连接.右连接和完整外部连接. 1)左连接:Left ...

  7. GNS3 模拟icmp目标不可达

    目标不可达: R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 end R2 f0/0: conf t int f0/ ...

  8. 小程序填坑:2018最新getPhoneNumber功能详解

    本篇博客主要详解getPhoneNumber组件的功能,填补网上那些到处是漏洞的博客.加上小程序官方本身也是满满的漏洞. 惯例先上总纲: ##主要内容 1.前端页面组件书写 2.JS内组件用法 3.接 ...

  9. bzoj 1912: [Apio2010]patrol 巡逻

    呵呵呵呵呵呵,自己画图,大概半个小时,觉的连上边会成环(是不是该交仙人掌了??)然后求环不重合部分最大就好了, 结果写了一坨DP,最后写不下去了,再次扒了题解. 发现我真的是个sb. k==1,直接是 ...

  10. 如何更改RStudio(或R)中的默认目录

    方法一: Session -> Set Working Directory -> Choose Directory ... or shortcut (Ctrl+Shift+H) 方法二 s ...