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. PAT (Advanced Level) 1124~1127:1124模拟 1125优先队列 1126欧拉通路 1127中序后序求Z字形层序遍历

    1124 Raffle for Weibo Followers(20 分) 题意:微博抽奖,有M个人,标号为1~M.从第S个人开始,每N个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给 ...

  2. vscode spring boot配置文件application.properties不提示解决方式

    背景 因实际的编程环境是jdk1.6,vscode安装了spring boot tools开发后,application.properties无法提示.spring boot tools的功能之一就是 ...

  3. 该虚拟机似乎正在使用中 如果该虚拟机未在使用请按获取所权T按钮获取他的所有权,否则,请按取消按钮以防损坏

    虚拟机出现如下情况 不能够正常过使用,解决办法如下:直接进入上图中配置文件的目录下,然后删除所有的lck结尾的文件夹,然后重新启动, 然后file打开这个文件即可.重新进入虚拟机开机.

  4. 题解:luogu P1247

    大概没你们说得复杂吧...... \(Part\;1\) \(Nim\)游戏 大家都对异或和感到懵逼吧(排除大佬),其实很简单,用\(SG\)函数打表计算即可解决: 抛个板子: void get_sg ...

  5. 云时代架构阅读笔记十一——数据库SQL优化

    网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 ...

  6. 页面的html调试

    点击页面按下键盘的F12,或者鼠标右键选择检查(N) 会弹出一个窗口,这个窗口就是调试窗口 如上图所示,第一个图标是标签元素选择器,点击使用后,在页面上移动,会在Elements的区域找到你鼠标选中的 ...

  7. 061、Java中利用return结束方法调用

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. SQLlite的olestr

    关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/ SQLite GUI客户端列表:http://www.sql ...

  9. Linux系统发现新恶意软件

    导读 安全研究人员发现了一种新的Linux恶意软件,它似乎是由中国黑客创建的,并被用作远程控制受感染系统的手段. 这个恶意软件命名为HiddenWasp,由用户模式rootkit,木马和初始部署脚本组 ...

  10. 关于spring-mvc.xml的静态资源的配置

    转 配置如下: <!-- 配置静态资源 --><mvc:resources location="/static/" mapping="/static/* ...