题目: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. 主成分分析、实例及R语言原理实现

    欢迎批评指正! 主成分分析(principal component analysis,PCA) 一.几何的角度理解PCA -- 举例:将原来的三维空间投影到方差最大且线性无关的两个方向(二维空间). ...

  2. thinkphp5.1 退出登陆操作

    使用Session:: 静态方法即可

  3. 日常杂记——C#验证码

    随机生成验证码,不能以图片的形式存在,所以需要将验证码图片以MemoryStream形式存储在内存的流当中,但是在使用时发现使用PictureBox控件无法显示内存流,所以需要先将流转化为图片,才可以 ...

  4. DataGridView添加的数据最后一步无法生效的问题。

    在做项目时,需要使用到大量固定的参数,使用时只需修改一部分定值即可,所以就选用DataGridView控件进行循环添加,数据添加完成,一切正常,但是在修改数据时发现,每一次修改的数据的操作的最后一步总 ...

  5. if判断,switch语句

    if ; else if; else; 判断操作: 格式示例: public class scanner { public static void main(String[] args){ int a ...

  6. 在CentOS7中安装scala-2.11.12

    从官网下载scala的相关版本 https://www.scala-lang.org/download/2.11.12.html 解压安装包 tar zxf scala-.tgz -C /usr/sc ...

  7. Unity UGUI 小知识

    1.有个控件叫Selectable 这个控件在button,slider等身上有,也可以自行添加,可通过API搜索所有带这个控件的物体统一控制. 2.实现ScrollView只使用Scrollbar操 ...

  8. Analysis of Servlet

    @WebServlet("/cdiservlet") public class NewServlet extends HttpServlet { private Message m ...

  9. go语言环境安装

    Go 是一个开源的编程语言,被设计成一门应用于搭载 Web 服务器,存储集群或类似用途的巨型中央服务器的系统编程语言. 1. 准备环境并安装依赖包 创建centos 7.6 64bit的虚拟机. 安装 ...

  10. 【c】多级指针

    一.一级指针 1.int *p,*p2; p是变量名,*表明是指针,指针指向地址. 在定义时初始化,如int *p_2 = &b; //定义一个指针,指针指向一个地址 先定义再初始化,如int ...