题目:Given nums = [2, 7, 11, 15], target = 9,
   Because nums[0] + nums[1] = 2 + 7 = 9,
   return [0, 1]

代码:(1.22)
   /**
   * @param {number[]} nums
   * @param {number} target
   * @return {number[]}
   */

   var twoSum = function(nums, target) {//给定两个参数nums,target
   var result = new Array(2);//定义result数组存放最后结果,长度为2
   for(var i = 0;i < nums.length;i++){ //横向遍历nums,如(第一遍得到i = 0,nums[0])
   for(var j =i + 1;j < nums.length;j++){
     //纵向遍历nums,如(上一行代码已经得到nums[0],此行代码第一遍得到nums[i + 1]即nums[1])
   if (nums[i] + nums[j] == target){//在第二层循环里判断nums[i] + nums[j]是否等于target
   result[0] = i;//如果等于target,则将i赋值给result[0]
   result[1] = j;//将j赋值给result[1];
   break;//只需要得到第一对nums[i] + nums[j]==tarfget,所以得到后跳出循环
   }
   }
   }
   return result;//返回result数组
   };
 

Leetcode——Two Sum(easy)的更多相关文章

  1. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  2. LeetCode--Array--Two sum (Easy)

    1.Two sum (Easy)# Given an array of integers, return indices of the two numbers such that they add u ...

  3. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  4. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. 【leetcode】Two Sum (easy)

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

  6. 【Leetcode】【Easy】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. LeetCode: 371 Sum of Two Integers(easy)

    题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...

  8. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  9. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

随机推荐

  1. sql 日志文件截断收缩

    use mydb ALTER DATABASE mydb SET RECOVERY SIMPLE WITH NO_WAIT ALTER DATABASE mydb SET RECOVERY SIMPL ...

  2. sql中的不常见查询

    1.对于CROSS APPLY 和 OUTER APPLY 的应用: CROSS APPLY 类似于INNER JOIN 但是,可以规定对于满足条件的数据需要关联几行,应用场景: 每个零件把第一个工单 ...

  3. Traceback (most recent call last): File "c:\program files (x86)\microsoft visual studio\2019\community\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\_pydevd_bundle\pyd

    某次编码,debug的时候突然突然突然给我报这个错: Traceback (most recent call last):   File "c:\program files (x86)\mi ...

  4. 【转】如何使用离线博客发布工具发布CSDN的博客文章

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  5. 2018-2019-2 20175213实验一 《Java开发环境的熟悉》实验报告

    第一部分实验要求:1 建立“自己学号exp1”的目录2 在“自己学号exp1”目录下建立src,bin等目录3 javac,java的执行在“自己学号exp1”目录4 提交 Linux或Window或 ...

  6. 循环输入到列表的基础方法 -----python-----

    print('向列表中添加元素(输入“#”结束)\n并查看添加完的列表') list1=[] while 1: username=input('>>>') if (username. ...

  7. netty(七) Handler的执行顺序

    Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通过Handler可以完成通讯报文的解码编码.拦截指定的报文.统一对日志错误进行处理.统一对 ...

  8. python3作业:模拟登录

    __author__ = "bin007" customer = {}#存储用户信息#处理用户信息文件try: with open('login.txt','r',encoding ...

  9. vue使用qrcode插件生成二维码

    参考:https://www.jianshu.com/p/d3883e020d99 步骤: 第一步:vue-cli下载插件 cnpm install --save qrcodejs2 第二步:组件中引 ...

  10. TZOJ 2703 Cow Digit Game(sg博弈)

    描述 Bessie is playing a number game against Farmer John, and she wants you to help her achieve victor ...